1. EntrySet循环遍历:JDK1.8以前的主要的遍历方式,效率最高
2.KeySet循环遍历:需要遍历两次,效率低
3. EntrySet迭代器循环遍历:可以删除元素
4. KeySet迭代器循环遍历:可以删除元素
5. 使用Lamda表达式循环遍历
6. Stream单线程遍历:代码简洁
7. Stream多线程遍历:效率最高推荐使用
//使用Lamda表达式循环遍历
map.forEach( (key, value)->{
System.out.println(key+ ":" + value);
} );
//Stream单线程遍历
map.entrySet().stream().forEach(entry-> {
System.out.println(entry.getKey() + ":" + entry.getValue());
});
//Stream多线程遍历
map.entrySet().stream().parallel().forEach(entry-> {
System.out.println(entry.getKey() + ":" + entry.getValue());
});
Stream多线程遍历