在这篇文章中,我将对Map的遍历方式做一个对比和总结,将分别从JAVA8之前和JAVA8做一个遍历方式的对比,亲测可行。

public class LambdaMap {

    private Map<String, Object> map = new HashMap<>();

    @Before
public void initData() {
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
map.put("key4", 4);
map.put("key5", 5);
map.put("key5", 'h');
} /**
* 遍历Map的方式一
* 通过Map.keySet遍历key和value
*/
@Test
public void testErgodicWayOne() {
System.out.println("---------------------Before JAVA8 ------------------------------");
for (String key : map.keySet()) {
System.out.println("map.get(" + key + ") = " + map.get(key));
}
System.out.println("---------------------JAVA8 ------------------------------");
map.keySet().forEach(key -> System.out.println("map.get(" + key + ") = " + map.get(key)));
} /**
* 遍历Map第二种
* 通过Map.entrySet使用Iterator遍历key和value
*/
@Test
public void testErgodicWayTwo() {
System.out.println("---------------------Before JAVA8 ------------------------------");
Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Object> entry = iterator.next();
System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue());
}
System.out.println("---------------------JAVA8 ------------------------------");
map.entrySet().iterator().forEachRemaining(item -> System.out.println("key:value=" + item.getKey() + ":" + item.getValue()));
} /**
* 遍历Map第三种
* 通过Map.entrySet遍历key和value,在大容量时推荐使用
*/
@Test
public void testErgodicWayThree() {
System.out.println("---------------------Before JAVA8 ------------------------------");
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue());
}
System.out.println("---------------------JAVA8 ------------------------------");
map.entrySet().forEach(entry -> System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue()));
} /**
* 遍历Map第四种
* 通过Map.values()遍历所有的value,但不能遍历key
*/
@Test
public void testErgodicWayFour() {
System.out.println("---------------------Before JAVA8 ------------------------------");
for (Object value : map.values()) {
System.out.println("map.value = " + value);
}
System.out.println("---------------------JAVA8 ------------------------------");
map.values().forEach(System.out::println); // 等价于map.values().forEach(value -> System.out.println(value));
} /**
* 遍历Map第五种
* 通过k,v遍历,Java8独有的
*/
@Test
public void testErgodicWayFive() {
System.out.println("---------------------Only JAVA8 ------------------------------");
map.forEach((k, v) -> System.out.println("key:value = " + k + ":" + v));
}
}

最新文章

  1. 【IScroll深入学习】突破移动端黑暗的利器(上)
  2. jQuery水平下拉菜单实现
  3. 348. Design Tic-Tac-Toe
  4. Android Service学习之IntentService 深入分析
  5. [LeetCode] Friend Circles 朋友圈
  6. 利用 jQuery 来验证密码两次输入是否相同
  7. scrapy splash 之一二
  8. Python中DataFrame关联
  9. 看进程的启动时间长度 + vmstat + jstack 应用
  10. sublime text3修改默认配置文件是失败的解决方法
  11. Chakra TypedArray代码实现笔记
  12. 树莓派学习笔记(5):成功实现NAS家庭服务器(流媒体播放、文件共享及下载机)
  13. 解决VMware安装ubuntu16.04后无法全屏的问题
  14. shell中引号的妙用
  15. hive--数据仓库
  16. DIV+CSS专题:十天学会DIV+CSS
  17. 使用 const 提高函数的健壮性
  18. while循环优化版本-for循环
  19. Incorrect string value: &#39;\xF0\x9F\x98\x84\xF0\x9F
  20. 使用Docker、CoreOS、Mesos部署可扩展的Web应用

热门文章

  1. winform中的小技巧【自用】
  2. NetworkComms V3 序列化器之Protobuf.net和 JSONSerializer
  3. thymeleaf数组下标
  4. upc组队赛6 GlitchBot【枚举】
  5. Django框架(十九)—— drf:序列化组件(serializer)
  6. rem 布局代码
  7. Javascript中的相等比较
  8. python2和python3中int整型数据的不同之处
  9. java性能调优01
  10. SpringCloud-技术专区-Zuul-使用指南