| By Joe Winchester | Article Rating: |
|
| August 15, 2006 05:00 PM EDT | Reads: |
13,223 |
A lot of the time I find myself writing code to iterate over a Map goes something like thisMap map = getMap();
// I'd like to read the keys and values of the maps
Iterator keys = map.keySet().iterator();
while(keys.hasNext()){
Object key = keys.next();
Object value = map.get(key);
}
This is basically because I've used the map to store things in a keyed fashion and want to iterate over the keys and the value for each. Problem is, each time I do it I find myself thinking how inefficient it must be. The keys iterator returns the keys so it has to walk the keys, however the get(key) has to lookup the key each time.Internally implementations like HashMap store keys and values as linked list pairs, so the API I'm wishing for it something like
Iterator keysAndValues = map.getKeysAndValues().iterator();
while(keys.hasNext()){
Pair keyAndValue = keysAndValues.next();
Object key = keyAndValue.getKey();
Object value = keyAndValue.getValue();
}
Dictionary d := Dictionary new.
d keysAndValuesDo: [ :key :value |]
This code does require the use of Blocks, but it could still be done in Java by introducing a new interface called Pair that was an inner class interface of Map. To make the new API non breaking it would need doing on a new interface called, say, Map2.
public interface Maps2 extends Map{
public Collection getKeysAndValues();
public interface Pair{
public Object getKey();
public Object getValue();
}
}
Each implementation of Map could do what it wants to implement Pair, so a default implementation could just return the key, hold a pointer to the Map, and do a get(key) for the getValue() method. However implementations like HashMap that internally use linked lists could be more efficient.
Published August 15, 2006 Reads 13,223
Copyright © 2006 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Joe Winchester
Joe Winchester, Editor-in-Chief of Java Developer's Journal, was formerly JDJ's longtime Desktop Technologies Editor and is a software developer working on development tools for IBM in Hursley, UK.
![]() |
Yuri 09/02/06 03:50:46 PM EDT | |||
Map.entrySet() enough said! |
||||
![]() |
adam 08/17/06 03:01:02 PM EDT | |||
Please look at the API before complaining that it lacks something. Map.entrySet() has been there since Maps came out. |
||||
![]() |
Ashish 08/16/06 05:32:52 AM EDT | |||
check out java.util.Map.Entry for (java.util.Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) { |
||||
![]() |
snoobabk 08/16/06 03:23:32 AM EDT | |||
Hi and by the way to everyone my previous post has a little sarcasm in it and there are equally smalltalk zealots, java zealots, python zealots, ruby zealots, lisp zealots etc etc There will always be :) but for people wanting to look at an open Smalltalk platform and other smalltalky things go to: http://www.squeak.org , http://www.opencroquet.org , http://www.object-arts.com/ |
||||
![]() |
snoobabk 08/16/06 03:06:26 AM EDT | |||
Hi I'm a long time Java developer but have been moving over to Smalltalk wherever I can. They'll jump all over you and think that Smalltalk is showing its age. A Map does have the ability to return a 'pair' of key-value bindings, it always has. It's an EntrySet that holds a key and value that can be obtained via a call such as map.entrySet(). Cheers |
||||
![]() |
SYS-CON Australia News Desk 08/15/06 05:24:37 PM EDT | |||
I've used the map to store things in a keyed fashion and want to iterate over the keys and the value for each. Problem is, each time I do it I find myself thinking how inefficient it must be. The keys iterator returns the keys so it has to walk the keys, however the get(key) has to lookup the key each time.Internally implementations like HashMap store keys and values as linked list pairs |
||||
![]() |
JDJ News Desk 08/15/06 03:51:13 PM EDT | |||
I've used the map to store things in a keyed fashion and want to iterate over the keys and the value for each. Problem is, each time I do it I find myself thinking how inefficient it must be. The keys iterator returns the keys so it has to walk the keys, however the get(key) has to lookup the key each time.Internally implementations like HashMap store keys and values as linked list pairs |
||||
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Kindle 2 vs Nook
- Why IBM’s Server Chief Got Busted
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Journal Opens "Readers' Choice Awards" Nominations
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Industry Experts Discuss the State of Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- It's the Java vs. C++ Shootout Revisited!
- The End of IT 1.0 As We Know It Has Begun
- An Introduction to Abbot
- Java Kicks Ruby on Rails in the Butt
- Interviewing Java Developers With Tears in My Eyes
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- 1st Annual Government IT Expo: Call for Papers Deadline July 15
- How to Diagnose Java Resource Starvation
- REA Is Where RIA Becomes the Norm
- Kindle 2 vs Nook
- Anatomy of a Java Finalizer
- Why IBM’s Server Chief Got Busted
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- The i-Technology Right Stuff
- JavaServer Faces (JSF) vs Struts
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- What's New in Eclipse?
































