HashMap 的7種遍歷方式
你知道的越多,不知道的就越多,業(yè)余的像一棵小草!
你來,我們一起精進!你不來,我和你的競爭對手一起精進!
編輯:業(yè)余草
blog.csdn.net/m0_46937429
推薦:https://www.xttblog.com/?p=5167

HashMap 遍歷
大體上可以分為4類:
1,迭代器
2,F(xiàn)orEach 遍歷
3,lambda 表達式遍歷
4,StreamsApi 遍歷
但是每種類型下有不同的實現(xiàn)方式,所以又可以分為7種:

案例demo
1,使用迭代器 EntrySet 的方式遍歷
@Test
//1,使用迭代器 EntrySet 的方式遍歷
public void demo1(){
//創(chuàng)建Map 對象
Map<Integer, String> map = new HashMap<>();
//添加數(shù)據(jù)
map.put(1,"嬌嬌");
map.put(2,"嬌嬌1");
map.put(3,"嬌嬌2");
map.put(4,"嬌嬌3");
map.put(5,"嬌嬌4");
map.put(5,"嬌嬌5");
//遍歷
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<Integer, String> next = iterator.next();
System.out.println(next.getKey());
System.out.println(next.getValue());
}
}
結(jié)果

2,使用迭代器的KeySet
@Test
// 2,使用迭代器的KeySet
public void demo1(){
//創(chuàng)建Map 對象
Map<Integer, String> map = new HashMap<>();
//添加數(shù)據(jù)
map.put(1,"嬌嬌");
map.put(2,"嬌嬌1");
map.put(3,"嬌嬌2");
map.put(4,"嬌嬌3");
map.put(5,"嬌嬌4");
map.put(5,"嬌嬌5");
//遍歷
Iterator<Integer> iterator = map.keySet().iterator();
while (iterator.hasNext()){
Integer key = iterator.next();
System.out.print(key);
System.out.print(map.get(key));
}
}
結(jié)果

3,使用 For Each EntrySet 的方式進行遍歷;
@Test
//3,使用 For Each EntrySet 的方式進行遍歷;
public void demo1(){
//創(chuàng)建Map 對象
Map<Integer, String> map = new HashMap<>();
//添加數(shù)據(jù)
map.put(1,"嬌嬌");
map.put(2,"嬌嬌1");
map.put(3,"嬌嬌2");
map.put(4,"嬌嬌3");
map.put(5,"嬌嬌4");
map.put(5,"嬌嬌5");
//遍歷
for (Map.Entry<Integer,String> entry: map.entrySet()
) {
System.out.println("entry.getKey() = " + entry.getKey());
System.out.println("entry.getValue() = " + entry.getValue());
}
}
結(jié)果

4,使用 For Each KeySet 的方式進行遍歷;
@Test
//4,使用 For Each KeySet 的方式進行遍歷;
public void demo1(){
//創(chuàng)建Map 對象
Map<Integer, String> map = new HashMap<>();
//添加數(shù)據(jù)
map.put(1,"嬌嬌");
map.put(2,"嬌嬌1");
map.put(3,"嬌嬌2");
map.put(4,"嬌嬌3");
map.put(5,"嬌嬌4");
map.put(5,"嬌嬌5");
//遍歷
for (Integer key: map.keySet()
) {
System.out.println(key);
System.out.println(map.get(key));
}
}
結(jié)果

5,使用 Lambda 表達式的方式進行遍歷;
@Test
//5,使用 Lambda 表達式的方式進行遍歷;
public void demo1() {
//創(chuàng)建Map 對象
Map<Integer, String> map = new HashMap<>();
//添加數(shù)據(jù)
map.put(1, "嬌嬌");
map.put(2, "嬌嬌1");
map.put(3, "嬌嬌2");
map.put(4, "嬌嬌3");
map.put(5, "嬌嬌4");
map.put(5, "嬌嬌5");
//遍歷
map.forEach((key,value) -> {
System.out.print(key);
System.out.print(value);
});
}
結(jié)果

6,使用 Streams API 單線程的方式進行遍歷;
@Test
//6,使用 Streams API 單線程的方式進行遍歷;
public void demo1() {
//創(chuàng)建Map 對象
Map<Integer, String> map = new HashMap<>();
//添加數(shù)據(jù)
map.put(1, "嬌嬌");
map.put(2, "嬌嬌1");
map.put(3, "嬌嬌2");
map.put(4, "嬌嬌3");
map.put(5, "嬌嬌4");
map.put(5, "嬌嬌5");
//遍歷
map.entrySet().stream().forEach((integerStringEntry -> {
System.out.println(integerStringEntry.getKey());
System.out.println(integerStringEntry.getValue());
}));
}
結(jié)果

7,使用 Streams API 多線程的方式進行遍歷。
@Test
//6,使用 Streams API 單線程的方式進行遍歷;
public void demo1() {
//創(chuàng)建Map 對象
Map<Integer, String> map = new HashMap<>();
//添加數(shù)據(jù)
map.put(1, "嬌嬌");
map.put(2, "嬌嬌1");
map.put(3, "嬌嬌2");
map.put(4, "嬌嬌3");
map.put(5, "嬌嬌4");
map.put(5, "嬌嬌5");
//遍歷
map.entrySet().parallelStream().forEach((integerStringEntry -> {
System.out.println(integerStringEntry.getKey());
System.out.println(integerStringEntry.getValue());
}));
}
結(jié)果

性能測試結(jié)果如下:

除了 Stream 的并行循環(huán),其他幾種遍歷方法的性能差別不大,但從簡潔性和優(yōu)雅性上來看,Lambda 和 Stream 無疑是最適合的遍歷方式。
評論
圖片
表情
