java中HashMap的七种遍历方式

java中HashMap的七种遍历方式

HashMap遍历方式分类

  • HashMap的多种遍历方式从大体中归类 , 可以分为以下4类 :
  • 迭代器(Iterator)
  • For Each
  • Lambda (JDK 1.8 +)
  • Streams API (JDK 1.8 +)
  • 但是每种方式又有不同的实现类型 :
  • 使用迭代器(Iterator)EntrySet / KeySet 的方式进行遍历;
  • 使用 For Each EntrySet / For Each KeySet 的方式进行遍历;
  • 使用 Lambda 表达式的方式进行遍历;
  • 使用 Streams API 单线程 / 多线程 的方式进行遍历;

迭代器(Iterator)EntrySet

  • HashMap<String , String> hashMap = new HashMap<>();
    
    hashMap.put("1","name");
    hashMap.put("2","age");
    
    Iterator<Map.Entry<String, String>> iterator = hashMap.entrySet().iterator();
    
    while (iterator.hasNext()) {
        Map.Entry<String, String> entry = iterator.next();
        Object key = entry.getKey();
        Object val = entry.getValue();
        System.out.println("key : " + key + "-----" + "val : " + val);
    }
    
    

迭代器(Iterator)KeySet

  • HashMap<String , String> hashMap = new HashMap<>();
    
    hashMap.put("1","name");
    hashMap.put("2","age");
    
    Iterator<String> iterator = hashMap.keySet().iterator();
    
    while (iterator.hasNext()) {
        String key = iterator.next();
        Object val = hashMap.get(key);
        System.out.println("key : " + key + "-----" + "val : " + val);
    }
    
    

For Each EntrySet

  • HashMap<String , String> hashMap = new HashMap<>();
    
    hashMap.put("1","name");
    hashMap.put("2","age");
    
    for (Map.Entry<String, String> entry : hashMap.entrySet()) {
        Object key = entry.getKey();
        Object val = entry.getValue();
        System.out.println("key : " + key + "-----" + "val : " + val);
    }
    
    

For Each KeySet

  • HashMap<String , String> hashMap = new HashMap<>();
    
    hashMap.put("1","name");
    hashMap.put("2","age");
    
    for (String key : hashMap.keySet()) {
        Object val = hashMap.get(key);
        System.out.println("key : " + key + "-----" + "val : " + val);
    }
    
    

Lambda

  • HashMap<String , String> hashMap = new HashMap<>();
    
    hashMap.put("1","name");
    hashMap.put("2","age");
    
    hashMap.forEach((key , val) -> System.out.println("key : " + key + "-----" + "val : " + val));
    
    

Streams API 单线程

  • HashMap<String , String> hashMap = new HashMap<>();
    
    hashMap.put("1","name");
    hashMap.put("2","age");
    
    hashMap.entrySet().stream().forEach((entry) -> {
        Object key = entry.getKey();
        Object val = entry.getValue();
        System.out.println("key : " + key + "-----" + "val : " + val);
    });
    
    

Streams API 多线程

  • HashMap<String , String> hashMap = new HashMap<>();
    
    hashMap.put("1","name");
    hashMap.put("2","age");
    
    hashMap.entrySet().stream().parallel().forEach((entry) -> {
        Object key = entry.getKey();
        Object val = entry.getValue();
        System.out.println("key : " + key + "-----" + "val : " + val);
    });
    
    
  • 我们不能在遍历Map时使用map.remove()方法 , 否则就会抛出异常 :

  • java.util.ConcurrentModificationException , 这种办法是非安全的 , 我们可以使用Iterator.remove() ,或者是Lambda 中的 removeIf() , 或者是Stream 中的 filter() 过滤或者删除相关数据

  • 38
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT枫斗者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值