What are the ways to traverse Map data structures in Java? What are the differences and performance of various usage modes?

1. There are four Map traversal modes

1.1. EntrySet traversal

The entrySet traversal is the most common Map traversal. It is usually used when both the Map key and the Map value are required. The method of traversal is divided into two steps:

1. Directly call the entrySet method of the Map object to obtain the Entry object.

2. Obtain the key and value from the getKey() and getValue() methods of the Entry object.

for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
    System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
}
Copy the code

Obtain keys or values from the Map object directly

If you only use keys or values in the Map object, this method is intuitive. The keySet() method obtains all keys in the Map, and the values() method obtains all values. The code is as follows:

For (Integer key: map.keyset ()) {system.out.println ("key = "+ key); For (Integer value: map.values()) {system.out.println ("key = "+ value); }Copy the code

1.3. Use the Iterator

Iterator traversal is friendly if nodes are inserted or deleted during Map traversal.

Iterator<Map.Entry<Integer, Integer>> it = map.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry<Integer, Integer> entry = it.next();
    System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
}
Copy the code

1.4. Java8 Lambda

The Java8 Lambda approach makes the syntax more concise.

2. Performance comparison

Public class Main {public static void Main (String[] args) {write your code here Integer> map = new HashMap<Integer, Integer>(); for (int i=0; i<100000; i++) { map.put(i,i); } Main main = new Main(); long time1 = System.currentTimeMillis(); main.loop1(map); long time2 = System.currentTimeMillis(); main.loop2(map); long time3 = System.currentTimeMillis(); main.loop3(map); long time4 = System.currentTimeMillis(); main.loop4(map); long time5 = System.currentTimeMillis(); System.out.println("time1:" + (time2-time1)); System.out.println("time2:" + (time3-time2)); System.out.println("time3:" + (time4-time3)); System.out.println("time4:" + (time5-time4)); Void loop1(Map<Integer, Integer> Map) {for (map.entry <Integer, Integer> Entry: map.entrySet()) { // Integer key = entry.getKey(); // Integer value = entry.getValue(); System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue()); Void loop2(Map<Integer, Integer> Map) {void loop2(Map<Integer, Integer> Map); map.keySet()) { System.out.println("key = " + key); For (Integer value: map.values()) {system.out.println ("key = "+ value); Void loop3(Map<Integer, Integer> Map) {Iterator< map.entry <Integer, Integer>> it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry<Integer, Integer> entry = it.next(); System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue()); } // 4. Java8 Lambda // Java8 provides Lambda expression support, and the syntax looks more concise, Void loop4(Map<Integer, Integer> Map) {map.foreach ((key, value) -> { System.out.println("key = " + key + ", value = " + value); }); }}Copy the code

The performance comparison results of 10000 times are as follows:

The first

loop1:590
loop2:1103
loop3:396
loop4:342
Copy the code

The 2nd

loop1:677
loop2:1121
loop3:538
loop4:357
Copy the code

The third time

loop1:823
loop2:1147
loop3:442
loop4:391
Copy the code

4 times

loop1:714
loop2:1126
loop3:450
loop4:322
Copy the code

The fifth time

loop1:639
loop2:1069
loop3:420
loop4:367
Copy the code

This shows: using java8 LAMDA expression, the Map object for traversal, the highest performance, independent access to keys, values due to two traversal, may lead to low performance, after the code career can use more Map lamDA expression, code more concise, more efficient oh!!