HashMap的遍历方式
从大方向来说,HashMap遍历方式可以分为4类:
- 迭代器(Iterator)方式遍历;
- For Each方式遍历;
- Lambda表达式遍历(jdk1.8之后);
- Streams API遍历(jdk1.8之后)。
在这4类中,又存在不同的遍历方式,因此可以细分为7类:
- 使用迭代器(Iterator)EntrySet的方式遍历;
- 使用迭代器(Iterator)KeySet的方式遍历;
- For Each EntrySet的方式遍历;
- For Each KeySet的方式遍历;
- 使用Lambda表达式的方式遍历;
- 使用Streams API 单线程的方式遍历;
- 使用Streams API 多线程的方式遍历。
具体代码例子
下面通过例子进行演示:
- 迭代器(Iterator)EntrySet方式
public class One {
//迭代器(Iterator)EntrySet方式
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1,"Java");
map.put(2,"C++");
map.put(3,"C");
map.put(4,"Python");
map.put(5,"Golang");
//遍历
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
while(iterator.hasNext()) {
Map.Entry<Integer, String> entry = iterator.next();
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
}
}
执行结果:
1
Java
2
C++
3
C
4
Python
5
Golang
2. 迭代器(Iterator)KeySet方式
public class Two {
//迭代器(Iterator)KeySet方式
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1,"Java");
map.put(2,"C++");
map.put(3,"C");
map.put(4,"Python");
map.put(5,"Golang");
//遍历
Iterator<Integer> iterator = map.keySet().iterator();
while(iterator.hasNext()) {
Integer key = iterator.next();
System.out.println(key);
System.out.println(map.get(key));
}
}
}
执行结果:
1
Java
2
C++
3
C
4
Python
5
Golang
3. For Each EntrySet方式
public class Three {
//ForEach EntrySet方式
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1,"Java");
map.put(2,"C++");
map.put(3,"C");
map.put(4,"Python");
map.put(5,"Golang");
//遍历
for (Map.Entry<Integer, String> entry : map.entrySet()){
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
}
}
执行结果:
1
Java
2
C++
3
C
4
Python
5
Golang
4. ForEach KeySet方式
public class Four {
//ForEach KeySet方式
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1,"Java");
map.put(2,"C++");
map.put(3,"C");
map.put(4,"Python");
map.put(5,"Golang");
//遍历
for (Integer key : map.keySet()){
System.out.println(key);
System.out.println(map.get(key));
}
}
}
执行结果:
1
Java
2
C++
3
C
4
Python
5
Golang
5. Lambda表达式方式
public class Five {
//Lambda表达式方式
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1,"Java");
map.put(2,"C++");
map.put(3,"C");
map.put(4,"Python");
map.put(5,"Golang");
//遍历
map.forEach((key, value) -> {
System.out.println(key);
System.out.println(value);
});
}
}
执行结果:
1
Java
2
C++
3
C
4
Python
5
Golang
6. Streams API 单线程方式
public class Six {
//Streams API 单线程方式
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1,"Java");
map.put(2,"C++");
map.put(3,"C");
map.put(4,"Python");
map.put(5,"Golang");
//遍历
map.entrySet().stream().forEach((entry) -> {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
});
}
}
执行结果:
1
Java
2
C++
3
C
4
Python
5
Golang
7. Streams API 多线程方式
public class Seven {
//Streams API 多线程方式
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1,"Java");
map.put(2,"C++");
map.put(3,"C");
map.put(4,"Python");
map.put(5,"Golang");
//遍历
map.entrySet().parallelStream().forEach((entry) -> {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
});
}
}
执行结果:
1
Java
2
C++
3
C
4
Python
5
Golang
总结:综合性能和安全性来看,我们应该尽量使用迭代器(Iterator)EntrySet 方式来遍历 Map 集合。
参考:https://mp.weixin.qq.com/s/zQBN3UvJDhRTKP6SzcZFKw