The best way to traverse the five kinds of HashMap, I do not believe you know all about it!

Original Address: https://www.javaguides.net/2020/03/5-best-ways-to-iterate-over-hashmap-in-java.html
Author: Ramesh Fadatare
translation: high trekking
https: // www .toutiao.com / a6803887957418705420 /

In this article, we will be the best five ways to traverse HashMap in the Java discussion by example.

  1. Use Iterator to traverse HashMap EntrySet

  2. Use Iterator to traverse HashMap KeySet

  3. For-each loop iteration using HashMap

  4. Lambda expressions to traverse using HashMap

  5. Use Stream API to traverse HashMap

By  https://www.javaguides.net/2018/07/java-8-lambda-expressions.html [6] for information about lambda expressions

1, the use of Iterator traversal HashMap EntrySet

package com.java.tutorials.iterations;  
  
import java.util.HashMap;  
import java.util.Iterator;  
import java.util.Map;  
import java.util.Map.Entry;  
  
/**  
 * 在 Java 中遍历 HashMap 的5种最佳方法  
 * @author Ramesh Fadatare  
 *  
 */  
public class IterateHashMapExample {  
    public static void main(String[] args) {  
        // 1. 使用 Iterator 遍历 HashMap 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());  
        }  
    }  
}

Output:

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

2, the use of Iterator traversal HashMap KeySet

package com.java.tutorials.iterations;  
  
import java.util.HashMap;  
import java.util.Iterator;  
import java.util.Map;  
  
/**  
 * 在 Java 中遍历 HashMap 的5种最佳方法  
 * @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");  
  
        // 2. 使用 Iterator 遍历 HashMap KeySet  
        Iterator < Integer > iterator = coursesMap.keySet().iterator();  
        while (iterator.hasNext()) {  
            Integer key = iterator.next();  
            System.out.println(key);  
            System.out.println(coursesMap.get(key));  
        }  
    }  
}

Output:

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

3, using the For-each loop through HashMap

package com.java.tutorials.iterations;  
  
import java.util.HashMap;  
import java.util.Map;  
  
/**  
 * 在 Java 中遍历 HashMap 的5种最佳方法  
 * @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. 使用 For-each 循环遍历 HashMap  
        for (Map.Entry < Integer, String > entry: coursesMap.entrySet()) {  
            System.out.println(entry.getKey());  
            System.out.println(entry.getValue());  
        }  
    }  
}

Output:

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

4, using Lambda expressions to traverse HashMap

package com.java.tutorials.iterations;  
  
import java.util.HashMap;  
import java.util.Map;  
  
/**  
 * 在 Java 中遍历 HashMap 的5种最佳方法  
 * @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");  
  
        // 4. 使用 Lambda 表达式遍历 HashMap  
        coursesMap.forEach((key, value) -> {  
            System.out.println(key);  
            System.out.println(value);  
        });  
    }  
}

Output:

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

5, using the Stream API to traverse HashMap

package com.java.tutorials.iterations;  
  
import java.util.HashMap;  
import java.util.Map;  
  
/**  
 * 在 Java 中遍历 HashMap 的5种最佳方法  
 * @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");  
  
        // 5. 使用 Stream API 遍历 HashMap  
        coursesMap.entrySet().stream().forEach((entry) - > {  
            System.out.println(entry.getKey());  
            System.out.println(entry.getValue());  
        });  
    }  
}

Output:

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

Focus on micro-channel public number: Java technology stack in the background reply: java, you can get the latest Java Tutorial I N chapter finishing, are dry.

I recommended to my blog to read more:

1. the Java the JVM, collections, multithreading, new series of tutorials

2. the Spring MVC, the Boot the Spring, the Spring series of tutorials Cloud

3. Maven, Git, the Eclipse, IDEA Intellij Tool Tutorial Series

4. the Java, the back-end architecture, Alibaba and other manufacturers face new questions

Life is beautiful, see tomorrow ~

Guess you like

Origin www.cnblogs.com/javastack/p/12610639.html