5 ways to iterate over a Map in java

1. Introduction

This post would demo 5 ways to iterate a Map in java.

The 5 ways include:

  • traverse map using map.keySet iterator
  • traverse map using map.values iterator
  • traverse map using map.entrySet iterator
  • traverse map using java 8 lambda
  • traverse map using java stream of EntrySet

1. Environments

  • Java 1.8

2. Example codes

2.1 Setup a utility method to build a Map

private static Map<Integer,String> buildMap(List<String> names) {
    Map<Integer,String> result = new HashMap<>();
    for(int i=0;i<names.size();i++) {
        result.put(i,names.get(i));
    }
    return result;
}

2.2 traverse map using map.keySet iterator

private static void traverseMap1(Map<Integer,String> map) {
    for(Iterator<Integer> iter = map.keySet().iterator();iter.hasNext();) {
        Integer key = iter.next();
        String nextValue = map.get(key);
        System.out.println("map of ("+key+"-->"+nextValue+")");
    }
}

2.3 traverse map using map.values iterator

private static void traverseMap2(Map<Integer,String> map) {
    for(String value:map.values()) {
        System.out.println(value);
    }
}

Notice this method can only print values of the Map.

2.4 traverse map using map.entrySet iterator

private static void traverseMap3(Map<Integer,String> map) {
    Set<Map.Entry<Integer,String>> entries = map.entrySet();
    for(Map.Entry<Integer,String> entry:entries) {
        System.out.println(entry.getKey()+"-->"+entry.getValue());
    }
}

2.5 traverse map using java 8 lambda

private static void traverseMap4(Map<Integer,String> map) {
    //using java 8 lambda
    map.forEach((k,v)-> System.out.println(k+"-->"+v));
}

2.6 traverse map using java stream of EntrySet

private static void traverseMap5(Map<Integer,String> map) {
    //using java 8 stream and entrySet
    map.entrySet().stream().forEach(entrySet->
            System.out.println(entrySet.getKey()+"-->"+entrySet.getValue()));
}

2.7 test the 5 methods

public static void main(String[] args) {
    Map<Integer,String> map = buildMap(Arrays.asList("Jack","Mike","Tom","Jerry"));
    System.out.println("traverse map using map.keySet iterator");
    traverseMap1(map);
    System.out.println("\ntraverse map using map.values iterator");
    traverseMap2(map);
    System.out.println("\ntraverse map using map.entrySet iterator");
    traverseMap3(map);
    System.out.println("\ntraverse map using java 8 lambda");
    traverseMap4(map);
    System.out.println("\ntraverse map using java stream of EntrySet");
    traverseMap5(map);
}

Run the code, we got the result:

traverse map using map.keySet iterator
map of (0-->Jack)
map of (1-->Mike)
map of (2-->Tom)
map of (3-->Jerry)

traverse map using map.values iterator
Jack
Mike
Tom
Jerry

traverse map using map.entrySet iterator
0-->Jack
1-->Mike
2-->Tom
3-->Jerry

traverse map using java 8 lambda
0-->Jack
1-->Mike
2-->Tom
3-->Jerry

traverse map using java stream of EntrySet
0-->Jack
1-->Mike
2-->Tom
3-->Jerry

3. summary

As you can see, the traverseMap4 is the most concise one.

In this post we demoed how to iterate a Map by using various methods.You can find the whole code examples on github project, the class source code is located at this

You can find detail documents about the java stream here: