目录
HashMap 遍历从大的方向来说,可分为以下 4 类:
- 迭代器 (Iterator) 方式遍历;
- foreach 方式遍历;
- Lambda 表达式遍历 (JDK 1.8+);
- Streams API 遍历 (JDK 1.8+)。
但每种类型下又有不同的实现方式,因此具体的遍历方式又可以分为以下 7 种:
- 使用迭代器(Iterator)EntrySet 的方式进行遍历;
- 使用迭代器(Iterator)KeySet 的方式进行遍历;
- 使用 foreach EntrySet 的方式进行遍历;
- 使用 foreach KeySet 的方式进行遍历;
- 使用 Lambda 表达式的方式进行遍历;
- 使用 Streams API 单线程的方式进行遍历;
- 使用 Streams API 多线程的方式进行遍历。
1.使用迭代器 (Iterator) EntrySet 的方式进行遍历
class HashMapTest {
public static void main(String[] args) {
Map<Integer, String> hashMap = new HashMap<>();
hashMap.put(1, "Java");
hashMap.put(2, "C++");
hashMap.put(3, "Python");
// Iterator entrySet
Iterator<Map.Entry<Integer, String>> iterator = hashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, String> entry = iterator.next();
System.out.println("(" + entry.getKey() + ", " + entry.getValue() + ")");
}
}
}
输出结果如下,下面几种遍历方式的结果于下面的一样,因此将不再展示。
(1, Java)
(2, C++)
(3, Python)
2.使用迭代器 (Iterator) KeySet 的方式进行遍历
class HashMapTest {
public static void main(String[] args) {
Map<Integer, String> hashMap = new HashMap<>();
hashMap.put(1, "Java");
hashMap.put(2, "C++");
hashMap.put(3, "Python");
// Iterator keySet
Iterator<Integer> iterator = hashMap.keySet().iterator();
while (iterator.hasNext()) {
Integer key = iterator.next();
System.out.println("(" + key + ", " + hashMap.get(key) + ")");
}
}
}
3.使用 foreach EntrySet 的方式进行遍历
class HashMapTest {
public static void main(String[] args) {
Map<Integer, String> hashMap = new HashMap<>();
hashMap.put(1, "Java");
hashMap.put(2, "C++");
hashMap.put(3, "Python");
// foreach entrySet
for (Map.Entry<Integer, String> entry : hashMap.entrySet()) {
System.out.println("(" + entry.getKey() + ", " + entry.getValue() + ")");
}
}
}
4.使用 foreach KeySet 的方式进行遍历
class HashMapTest {
public static void main(String[] args) {
Map<Integer, String> hashMap = new HashMap<>();
hashMap.put(1, "Java");
hashMap.put(2, "C++");
hashMap.put(3, "Python");
// foreach keySet
for (Integer key : hashMap.keySet()) {
System.out.println("(" + key + ", " + hashMap.get(key) + ")");
}
}
}
5.使用 Lambda 表达式的方式进行遍历
class HashMapTest {
public static void main(String[] args) {
Map<Integer, String> hashMap = new HashMap<>();
hashMap.put(1, "Java");
hashMap.put(2, "C++");
hashMap.put(3, "Python");
// Lambda
hashMap.forEach((key, value) -> {
System.out.println("(" + key + ", " + value + ")");
});
}
}
6.使用 Streams API 单线程的方式进行遍历
class HashMapTest {
public static void main(String[] args) {
Map<Integer, String> hashMap = new HashMap<>();
hashMap.put(1, "Java");
hashMap.put(2, "C++");
hashMap.put(3, "Python");
// Streams API 单线程
hashMap.entrySet().stream().forEach((entry) -> {
System.out.println("(" + entry.getKey() + ", " + entry.getValue() + ")");
});
}
}
7.使用 Streams API 多线程的方式进行遍历
class HashMapTest {
public static void main(String[] args) {
Map<Integer, String> hashMap = new HashMap<>();
hashMap.put(1, "Java");
hashMap.put(2, "C++");
hashMap.put(3, "Python");
// Streams API 多线程
hashMap.entrySet().parallelStream().forEach((entry) -> {
System.out.println("(" + entry.getKey() + ", " + entry.getValue() + ")");
});
}
}