Java Collection - seven traversal methods of HashMap

HashMap traversal can be divided into the following four categories from a general perspective:

  1. Iterator traversal;
  2. For Each traversal mode;
  3. Lambda expression traversal (JDK 1.8 +);
  4. Streams API traversal (JDK 1.8 +).

However, there are different implementation methods under each type, so the specific traversal methods can be divided into the following seven types:

  1. Iterator (iterator) EntrySet is used for traversal;
  2. Use Iterator KeySet to traverse;
  3. Use For Each EntrySet to traverse;
  4. Use For Each KeySet to traverse;
  5. Use Lambda expression to traverse;
  6. Use Streams API to traverse in a single thread manner;
  7. Use the Streams API to traverse in a multi-threaded manner.

1, Iterator EntrySet

Iterator<Map.Entry<Integer, String>> iterator = map.entrySet.iterator();
while(iterator.hasNext()) {
    Map.Entry<Integer, String> entry = iterator.next();
    System.out.println(entry.getKey());
    System.out.println(entry.getValue());
}

2, Iterator KeySet

Iterator<Integer> iterator = map.keySet().iterator();
while(iterator.hasNext()) {
    Integer Key = iterator.next();
    System.out.println(key);
    System.out.println(map.get(key));
}

3, ForEach EntrySet

for(Map.Entry<Integer, String> entry : map.entrySet()) {
    System.out.println(entry.getKey());
    System.out.println(entry.getValue());
}

Note: if you look at the bytecode, you can see that the code generated by the EntrySet traversed by the Iterator loop and the for loop is the same. They both create an traversal object Entry in the loop. Therefore, the efficiency of ForEach method is the same as that of Iterator method.

4, ForEach KeySet

for(Integer key : map.keySet()) {
    System.out.println(key);
    System.out.println(map.get(key));
}

5, Lambda

map.forEach((key, value) -> {
    System.out.println(key);
    System.out.println(value);
} );

6, Streams API single thread

map.entrySet().stream().forEach((entry) -> {
    System.out.println(entry.getKey());
    System.out.println(entry.getValue());
});

7, Streams API multithreading

map.entrySet().parallelStream().forEach((entry) -> {
    System.out.println(entry.getKey());
    System.out.println(entry.getValue());
});

8, Performance comparison between EntrySet and KeySet

The reason why EntrySet has higher performance than KeySet is that KeySet uses Map Get (key), and Map Get (key) is equivalent to traversing the Map set again to query the value corresponding to the key. Why use the word "you"? That's because when you use an iterator or a for loop, you have actually traversed the Map collection once, so you can use Map Get (key) query is equivalent to traversing twice.

The EntrySet only traverses the Map set once, and then puts the key and value values of the object into the Entry object through the code "Entry < integer, string > Entry = iterator. Next()". Therefore, when obtaining the key and value values, there is no need to traverse the Map set again, just take values from the Entry object.

Therefore, the performance of EntrySet is twice as high as that of KeySet, because KeySet is equivalent to looping the Map set twice, while EntrySet only loops once.

9, Error prone point - delete elements during traversal

We cannot use the set map in traversal Remove() to delete data. This is an unsafe operation mode, and an error of "java.util.ConcurrentModificationException" will be reported.

But we can use iterator of iterator Remove () method to delete data, which is a safe way to delete collections.

Keywords: Java Back-end set

Added by Kunax on Wed, 02 Feb 2022 11:25:37 +0200