原文地址:https://www.javaguides.net/2020/03/5-best-ways-to-iterate-over-hashmap-in-java.html[1]
作者:Ramesh Fadatare[2]
翻譯:高行行
在本文中,我們將通過示例討論在 Java 上遍歷 HashMap[3] 的五種最佳方法。
- 使用 Iterator 遍歷 HashMap EntrySet
- 使用 Iterator 遍歷 HashMap KeySet
- 使用 For-each 循環迭代 HashMap
- 使用 Lambda 表達式[4]遍歷 HashMap
- 使用 Stream API[5] 遍歷 HashMap
通過 https://www.javaguides.net/2018/07/java-8-lambda-expressions.html [6]瞭解有關 lambda 表達式的信息
1. 使用 Iterator 遍歷 HashMap EntrySet
<code>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());
}
}
}/<code>
Output:
<code>1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework/<code>
2. 使用 Iterator 遍歷 HashMap KeySet
<code>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));
}
}
}/<code>
Output:
<code>1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework
/<code>
3. 使用 For-each 循環遍歷 HashMap
<code>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());
}
}
}/<code>
Output:
<code>1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework/<code>
4. 使用 Lambda 表達式遍歷 HashMap
<code>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);
});
}
}/<code>
Output:
<code>1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework/<code>
5. 使用 Stream API 遍歷 HashMap
<code>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());
});
}
}/<code>
Output:
<code>1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework/<code>
HashMap 的相關教程
- 集合框架-HashMap 類[7]
- 集合框架-LinkedHashMap 類[8]
- 集合框架-TreeMap 類[9]
- 集合框架-EnumMap[10]
- 集合框架-WeakHashMap[11]
- 集合框架-IdentityHashMap[12]
[1] https://www.javaguides.net/2020/03/5-best-ways-to-iterate-over-hashmap-in-java.html: https://www.javaguides.net/2020/03/5-best-ways-to-iterate-over-hashmap-in-java.html
[2] Ramesh Fadatare: https://www.blogger.com/profile/14691512106162803120
[3] HashMap: https://www.javaguides.net/2018/06/guide-to-hashmap-class.html
[4] Lambda 表達式: https://www.javaguides.net/2018/07/java-8-lambda-expressions.html
[5] Stream API: https://www.javaguides.net/2018/07/java-8-stream-api.html
[6] https://www.javaguides.net/2018/07/java-8-lambda-expressions.html : https://www.javaguides.net/2018/07/java-8-lambda-expressions.html
[7] 集合框架-HashMap 類: https://www.javaguides.net/2018/06/guide-to-hashmap-class.html
[8] 集合框架-LinkedHashMap 類: https://www.javaguides.net/2018/06/guide-to-linkedhashmap-class.html
[9] 集合框架-TreeMap 類: https://www.javaguides.net/2018/07/guide-to-treemap-class.html
[10] 集合框架-EnumMap: https://www.javaguides.net/2018/08/enummap-in-java.html
[11] 集合框架-WeakHashMap: https://www.javaguides.net/2018/08/weakhashmap-in-java.html
[12] 集合框架-IdentityHashMap: https://www.javaguides.net/2018/08/identityhashmap-in-java.html
閱讀更多 駭客與畫家 的文章