Welcome!

Java Authors: Michael Sheehan, Maureen O'Gara, Jonny Defh, Suresh Krishna Madhuvarsu, RealWire News Distribution

Related Topics: Java

Java: Article

Can Map Do A Better Job at Allowing Optimized Iteration Over Its Keys and Values Together?

A lot of the time I find myself writing code to iterate over a Map

A lot of the time I find myself writing code to iterate over a Map goes something like this

 Map 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();
}

This would need a new interface Pair that would have a getKey() and getVelue() method. It's basically similar to what Smalltalk does where you do code like

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.

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.

Comments (7) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
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(); ) {
java.util.Map.Entry entry = (java.util.Map.Entry) iter.next();
Object key = entry.getKey();
Object value = entry.getValue();
}

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.
Other people (Java zealots) reading this article may get the wrong impression of Smalltalkers when they read your article because its incorrect.

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