• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2323
  • Last Modified:

Options for iterating through maps

I am using an iterator to loop through a map to print it’s contents. Are there other simpler options for getting an iterator for a map, other than my below example?
   
Set entries = map.entrySet();
    Iterator iterator = entries.iterator();
    while (iterator.hasNext()) {
      Map.Entry entry = (Map.Entry)iterator.next();
      System.out.println("Key:" + entry.getKey() + " : " + entry.getValue());
    }
0
rayskelton
Asked:
rayskelton
  • 3
  • 2
  • 2
  • +2
2 Solutions
 
CEHJCommented:
Not really. But you could do

System.out.println(iterator.next());
0
 
aozarovCommented:
You can print the whole map by doing:
System.out.println(map);

Which will print it in this format "[key1=value1, key2=value2,... ]"
0
 
objectsCommented:
you can use the following:

Iterator iterator = map.keySet().iterator();
while (iterator.hasNext()) {
  Object key = iterator.next();
  Object value = map.get(key);
  System.out.println("Key:" + key + " : " + value);
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
aozarovCommented:
>> Are there other simpler options for getting an iterator for a map
objects, Is your suggestion simpler then the one provided rayskelton
0
 
rayskeltonAuthor Commented:
Of the two available options, is there a performance hit for one against the other?

    // get iterator of map through map.entries
    Set entries = map.entrySet();
    Iterator iterator = entries.iterator();
    while (iterator.hasNext()) {
      Map.Entry entry = (Map.Entry)iterator.next();
//      dest.write("Key:" + entry.getKey() + " : Message:" + entry.getValue() + "\n");
      dest.write(entry.getKey() + " :: " + entry.getValue() + "\n");
//      dest.write("Message:" + entry.getValue() + "\n");
    }
   
    Iterator iterator1 = map.keySet().iterator();
    dest.write("\nOption 2 for map writing\n");
    while (iterator1.hasNext()) {
      Object key = iterator1.next();
      Object value = map.get(key);
      dest.write(key + " :: " + value + "\n");
    }
 
0
 
aozarovCommented:
Yours (Iterating the entries) should perform better :-)
0
 
doronbCommented:
I use the following snippet so often that I've made it a macro-template in my JBuilder:

    Map.Entry entry = null;
    for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
      entry = (Map.Entry)iter.next();
      Object key = entry.getKey();
      Object value = entry.getValue();
      // Do whatever with the key and value..
    }
0
 
objectsCommented:
Doubt there would be any significant performance difference, suggest you benchmark if you are dealing with large sets and performance is an issue
0
 
rayskeltonAuthor Commented:
thanks for all advice
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now