E-Commerce 2.0
Brian wrote: I think
we're heading in the
right direction, but
we've still...
May. 16, 2008 02:55 AM
|
|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SOA World Conference
Virtualization Conference $200 Savings Expire May 16, 2008... – Register Today!
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON Java Industry News
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
By: Joe Winchester
Aug. 15, 2006 05:00 PM
Digg This!
Map map = getMap(); 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(); 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. 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{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.
LATEST JAVA STORIES & POSTS
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING JAVA NEWS
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||