| By Joe Winchester | Article Rating: |
|
| August 15, 2006 05:00 PM EDT | Reads: |
15,192 |
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 15,192
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 |
||||
- It's the Java vs. C++ Shootout Revisited!
- Patterns for Building High Performance Applications
- Asynchronous Logging Using Spring
- Java for Programmers (2nd Edition)
- Cross-Platform Mobile Website Development – a Tool Comparison
- Three Buzzwords That Every CIO Hears but One They Should Listen To
- Write Once Run Anywhere or Cross Platform Mobile Development Tools
- Immersing into JavaScript Frameworks
- Workday Reportedly Prepping to Go Public
- Cloud Expo New York: The Java EE 7 Platform - Developing for the Cloud
- Book Review: Sams Teach Yourself Java in 24 Hours
- OpenOffice.com Lives
- Book Excerpt: Introducing HTML5
- Adobe Sends Flex to the Apache Foundation
- Five Years Waiting for JRE 7: Is It Justified? (Part 1)
- Book Excerpt: Java Application Profiling Tips and Tricks
- i-Technology in 2012: Five Industry Predictions
- It's the Java vs. C++ Shootout Revisited!
- Patterns for Building High Performance Applications
- OpenXava 4.3: Rapid Java Web Development
- The Next Web Architecture
- Asynchronous Logging Using Spring
- Java for Programmers (2nd Edition)
- Is Write Once Run Anywhere Ever Going to Be a Reality?
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- JavaServer Faces (JSF) vs Struts
- The i-Technology Right Stuff
- 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
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- What's New in Eclipse?
- i-Technology Predictions for 2007: Where's It All Headed?





















