• 1. Use Iterator to traverse the HashMap EntrySet

  • 2. Use Iterator to traverse the HashMap KeySet

  • 3. Iterate over the HashMap using a for-each loop

  • 4. Use Lambda expressions to traverse the HashMap

  • 5. Use the Stream API to traverse the HashMap

  • 6. The final

In this article, we’ll use examples to discuss the five best ways to traverse a HashMap on Java.

  1. Use Iterator to traverse the HashMap EntrySet

  2. Use Iterator to traverse the HashMap KeySet

  3. Iterate over the HashMap using a for-each loop

  4. Use Lambda expressions to traverse the HashMap

  5. Use the Stream API to traverse the HashMap

1. Use Iterator to traverse the HashMap EntrySet

package com.java.tutorials.iterations; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; * @author Ramesh Fadatare ** / public class IterateHashMapExample {public static void main(String[] args) { // 1. EntrySet Map < Integer, String > coursesMap = new HashMap < Integer, String > (); coursesMap.put(1, "C"); coursesMap.put(2, "C++"); coursesMap.put(3, "Java"); coursesMap.put(4, "Spring Framework"); coursesMap.put(5, "Hibernate ORM framework"); Iterator < Entry < Integer, String >> iterator = coursesMap.entrySet().iterator(); while (iterator.hasNext()) { Entry < Integer, String > entry = iterator.next(); System.out.println(entry.getKey()); System.out.println(entry.getValue()); }}}Copy the code

Output:

1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework
Copy the code

2. Use Iterator to traverse the HashMap KeySet

package com.java.tutorials.iterations; import java.util.HashMap; import java.util.Iterator; import java.util.Map; * @author Ramesh Fadatare ** / public class IterateHashMapExample {public static void main(String[] args) { Map < Integer, String > coursesMap = new HashMap < Integer, String > (); coursesMap.put(1, "C"); coursesMap.put(2, "C++"); coursesMap.put(3, "Java"); coursesMap.put(4, "Spring Framework"); coursesMap.put(5, "Hibernate ORM framework"); KeySet Iterator < Integer > Iterator = coursesmap.keyset ().iterator(); while (iterator.hasNext()) { Integer key = iterator.next(); System.out.println(key); System.out.println(coursesMap.get(key)); }}}Copy the code

Output:

1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework
Copy the code

3. Iterate over the HashMap using a for-each loop

package com.java.tutorials.iterations; import java.util.HashMap; import java.util.Map; * @author Ramesh Fadatare ** / public class IterateHashMapExample {public static void main(String[] args) { Map < Integer, String > coursesMap = new HashMap < Integer, String > (); coursesMap.put(1, "C"); coursesMap.put(2, "C++"); coursesMap.put(3, "Java"); coursesMap.put(4, "Spring Framework"); coursesMap.put(5, "Hibernate ORM framework"); // 3. Loop through HashMap For (map. Entry < Integer, String > Entry: coursesMap.entrySet()) { System.out.println(entry.getKey()); System.out.println(entry.getValue()); }}}Copy the code

Output:

1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework
Copy the code

4. Use Lambda expressions to traverse the HashMap

package com.java.tutorials.iterations; import java.util.HashMap; import java.util.Map; * @author Ramesh Fadatare ** / public class IterateHashMapExample {public static void main(String[] args) { Map < Integer, String > coursesMap = new HashMap < Integer, String > (); coursesMap.put(1, "C"); coursesMap.put(2, "C++"); coursesMap.put(3, "Java"); coursesMap.put(4, "Spring Framework"); coursesMap.put(5, "Hibernate ORM framework"); ForEach ((key, value) -> {system.out.println (key); System.out.println(value); }); }}Copy the code

Output:

1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework
Copy the code

5. Use the Stream API to traverse the HashMap

package com.java.tutorials.iterations; import java.util.HashMap; import java.util.Map; * @author Ramesh Fadatare ** / public class IterateHashMapExample {public static void main(String[] args) { Map < Integer, String > coursesMap = new HashMap < Integer, String > (); coursesMap.put(1, "C"); coursesMap.put(2, "C++"); coursesMap.put(3, "Java"); coursesMap.put(4, "Spring Framework"); coursesMap.put(5, "Hibernate ORM framework"); Stream ().foreach ((entry) - > {system.out.println (entry.getKey()); System.out.println(entry.getValue()); }); }}Copy the code

Output:

1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework
Copy the code

6. The final

Java has a wide range of knowledge, and interview questions cover a wide range of topics, including: Java foundation, Java concurrency, JVM, MySQL, data structures, algorithms, Spring, microservices, MQ, etc., involved in the knowledge of how huge, so we often don’t know how to start in the review, today small editor to bring you a set of Java interview questions, questions library is very comprehensive. Including Java basics, Java Collections, JVM, Java concurrency, Spring buckets, Redis, MySQL, Dubbo, Netty, MQ, etc., including Java back-end knowledge 2000 +

Concern public number: “programmer Bai Nannan” to obtain