当前位置:   article > 正文

java中HashMap的七种遍历方式_java hashmap遍历

java hashmap遍历

感兴趣的话大家可以关注一下公众号 : 猿人刘先生 , 欢迎大家一起学习 , 一起进步 , 一起来交流吧!

1.HashMap遍历方式分类

HashMap的多种遍历方式从大体中归类 , 可以分为以下4类 :

  1. 迭代器(Iterator)

  2. For Each

  3. Lambda (JDK 1.8 +)

  4. Streams API (JDK 1.8 +)

但是每种方式又有不同的实现类型 :

  1. 使用迭代器(Iterator)EntrySet / KeySet 的方式进行遍历;

  2. 使用 For Each EntrySet / For Each KeySet 的方式进行遍历;

  3. 使用 Lambda 表达式的方式进行遍历;

  4. 使用 Streams API 单线程 / 多线程 的方式进行遍历;

接下来我们看每种方式的具体实现代码

1.1. 迭代器(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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

1.2. 迭代器(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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

1.3. 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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

1.4. 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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

1.5. 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));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

1.6.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);
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

1.7.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);
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

注 : 我们不能在遍历Map时使用map.remove()方法 , 否则就会抛出异常 :

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/564532
推荐阅读
相关标签
  

闽ICP备14008679号