Summary of 5 Ways of Traversing Map Set in Java

  • 2021-08-12 02:47:19
  • OfStack

Mode 1 uses iterator traversal through Map. keySet


@Test
public void testHashMap1() {
 Map<Integer, String> map = new HashMap<>();
 map.put(001, "Java");
 map.put(002, " Database ");
 map.put(003, "Vue");
 System.out.println(map);

 //  Pass Map.keySet Use iterator Traversal key, And then through key Get the corresponding value Value 
 Iterator<Integer> iterator = map.keySet().iterator();
 while (iterator.hasNext()) {
 Integer key = iterator.next();
 String value = map.get(key);
 System.out.println("key = " + key + ", value = " + value);
 }
}

Results:

{1=Java, 2=Database, 3=Vue}
key = 1, value = Java
key = 2, value = database
key = 3, value = Vue

Mode 2 uses iterator traversal through Map. entrySet


@Test
public void testHashMap2() {
 Map<Integer, String> map = new HashMap<>();
 map.put(001, "Java");
 map.put(002, " Database ");
 map.put(003, "Vue");
 System.out.println(map);

 //  Pass Map.entrySet Use iterator Traversal key And value ; Attention  Set entrySet() Returns all key-value To constitute Set Set 
 Iterator<Map.Entry<Integer, String>> entries = map.entrySet().iterator();
 while (entries.hasNext()) {
 Map.Entry<Integer, String> entry = entries.next();
 System.out.println(entry);
 }
}

Results:

{1=Java, 2=Database, 3=Vue}
1=Java
2 = Database
3=Vue

Mode 3 traverses through Map. keySet


@Test
public void testHashMap3() {
 Map<Integer, String> map = new HashMap<>();
 map.put(001, "Java");
 map.put(002, " Database ");
 map.put(003, "Vue");
 System.out.println(map);

 //  Pass Map.keySet Traversal key, And then through key Get the corresponding value Value 
 for (Integer key : map.keySet()) {
 System.out.println("key = " + key + ", value = " + map.get(key));
 }
}

Results:

{1=Java, 2=Database, 3=Vue}
key = 1, value = Java
key = 2, value = database
key = 3, value = Vue

Mode 4 iterates entries through For-Each, using Map. entrySet traversal


@Test
public void testHashMap4() {
 Map<Integer, String> map = new HashMap<>();
 map.put(001, "Java");
 map.put(002, " Database ");
 map.put(003, "Vue");
 System.out.println(map);

 //  Use For-Each Iteration entries , through Map.entrySet Traversal key And value
 for (Map.Entry<Integer, String> entry : map.entrySet()) {
 System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
 }
}

{1=Java, 2=Database, 3=Vue}
key = 1, value = Java
key = 2, value = database
key = 3, value = Vue

Mode 5 uses the lambda expression forEach traversal


@Test
public void testHashMap5() {
 Map<Integer, String> map = new HashMap<>();
 map.put(001, "Java");
 map.put(002, " Database ");
 map.put(003, "Vue");
 System.out.println(map);

	//  Use lambda Expression forEach Traversal 
 map.forEach((k, v) -> System.out.println("key = " + k + ", value = " + v));
}

forEach source code


default void forEach(BiConsumer<? super K, ? super V> action) {
 Objects.requireNonNull(action);
 for (Map.Entry<K, V> entry : entrySet()) {
  K k;
  V v;
  try {
  k = entry.getKey();
  v = entry.getValue();
  } catch(IllegalStateException ise) {
  // this usually means the entry is no longer in the map.
  throw new ConcurrentModificationException(ise);
  }
  action.accept(k, v);
 }
 }

As can be seen from the source code, this new feature adds a shell to the traditional iterative method, but makes the code simpler. (Recommended for development)

Summarize

It is recommended to use entrySet to traverse the Map class collection KV (the fourth method in this article) instead of keySet.

keySet is actually traversed twice, the first time is converted into Iterator object, and the second time is to take out value value corresponding to key from hashMap. However, entrySet is only traversed once, and key and value are put into entry, which is more efficient.

values () returns a collection of V values, which is an list collection object; keySet () returns a collection of K values, which is an Set collection object; entrySet () returns a combined collection of K-V values.

For JDK8, the recommended method is Map. forEach (the fifth method in this article).


Related articles: