YOUR FEEDBACK
E-Commerce 2.0
Brian wrote: I think we're heading in the right direction, but we've still...


2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TOP THREE LINKS YOU MUST CLICK ON


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

Digg This!

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.
About Joe Winchester
Joe Winchester, JDJ's Desktop Technologies Editor, is a software developer working on development tools for IBM in Hursley, UK.

Yuri wrote: Map.entrySet() enough said!
read & respond »
adam wrote: Please look at the API before complaining that it lacks something. Map.entrySet() has been there since Maps came out.
read & respond »
Ashish wrote: check out java.util.Map.Entry for (java.util.Iterator iter = map.entrySet().ite rator(); iter.hasNext(); ) { java.util.Map.Entry entry = (java.util.Map.Entry) iter.next(); Object key = entry.getKey(); Object value = entry.getValue(); }
read & respond »
snoobabk wrote: 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 , h ttp://www.opencroquet.org , http://www.object-arts. com/
read & respond »
snoobabk wrote: 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
read & respond »
SYS-CON Australia News Desk wrote: 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
read & respond »
JDJ News Desk wrote: 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
read & respond »
LATEST JAVA STORIES & POSTS
Case Study: Java and the Mac
This is the story of a Mac application developer (okay - it's about two of them) who set out on a quest to find an application development tool based on Java so his boss would let him develop on the Mac platform, which he loved. There was only one catch - he had to find a tool th
A Lightweight Approach to SOA and BPM in Java Using jBPM
SOA is mostly associated with technologies such as BPEL, SCA and Web Services. But does SOA really imply these technologies? In this session we will show how you can use the service oriented approach while staying inside the Java world. jBPM is a powerful lightweight framework th
JavaOne 2008: Uncommon Java Bugs
Any large Java source base can have insidious and subtle bugs. Every experienced Java programmer knows that finding and fixing these bugs can be difficult and costly. Fortunately, there are a large number of free open source Java tools available that can be used to find and fix d
The 4 Core Principles of Agile Programming
One of the things I really enjoy at the moment is the recognition and adoption of agile programming as a fully fledged powerful way to deliver quality software projects. As its figurehead is a group of very talented individuals who have created the agile manifesto (http://agilema
JavaOne 2008: Sun Adds Comprehensive Video Capabilities to JavaFX
Sun Microsystems announced it has entered into a multi-year agreement with On2 Technologies to add comprehensive video capabilities, using On2 Technologies TrueMotion video codecs, to Sun's JavaFX, a family of products for creating Rich Internet Applications (RIAs) with immersive
JavaOne Archives - Dvorak Comments on AMD Intel Lawsuit on SYS-CON.TV
Conference in San Francisco. Dvorak held forth on a number of topics, including the new AMD/Intel lawsuit, the viability of Java and Sun, the value of (or lack thereof) of corporate PR, and whether or not a new book about Silicon Valley is really worth reading.
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON FEATURED WHITEPAPERS

ADS BY GOOGLE
BREAKING JAVA NEWS
KongZhong Corporation Reports Unaudited First Quarter 2008 Financial Results
KongZhong Corporation , a leading wireless value-added services (WVAS) and wireless media co