温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java中遍历Map集合的方法有哪些

发布时间:2021-02-01 09:33:10 来源:亿速云 阅读:209 作者:小新 栏目:开发技术

这篇文章主要介绍了Java中遍历Map集合的方法有哪些,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

方式一 通过Map.keySet使用iterator遍历

@Test
public void testHashMap1() {
 Map<Integer, String> map = new HashMap<>();
 map.put(001, "Java");
 map.put(002, "数据库");
 map.put(003, "Vue");
 System.out.println(map);

 // 通过Map.keySet使用iterator遍历key,然后通过key得到对应的value值
 Iterator<Integer> iterator = map.keySet().iterator();
 while (iterator.hasNext()) {
 Integer key = iterator.next();
 String value = map.get(key);
 System.out.println("key = " + key + ", value = " + value);
 }
}

结果:

{1=Java, 2=数据库, 3=Vue}
key = 1, value = Java
key = 2, value = 数据库
key = 3, value = Vue

方式二 通过Map.entrySet使用iterator遍历

@Test
public void testHashMap2() {
 Map<Integer, String> map = new HashMap<>();
 map.put(001, "Java");
 map.put(002, "数据库");
 map.put(003, "Vue");
 System.out.println(map);

 // 通过Map.entrySet使用iterator遍历key和value;注意 Set entrySet():返回所有key-value对构成的Set集合
 Iterator<Map.Entry<Integer, String>> entries = map.entrySet().iterator();
 while (entries.hasNext()) {
 Map.Entry<Integer, String> entry = entries.next();
 System.out.println(entry);
 }
}

结果:

{1=Java, 2=数据库, 3=Vue}
1=Java
2=数据库
3=Vue

方式三 通过Map.keySet遍历

@Test
public void testHashMap3() {
 Map<Integer, String> map = new HashMap<>();
 map.put(001, "Java");
 map.put(002, "数据库");
 map.put(003, "Vue");
 System.out.println(map);

 // 通过Map.keySet遍历key,然后通过key得到对应的value值
 for (Integer key : map.keySet()) {
 System.out.println("key = " + key + ", value = " + map.get(key));
 }
}

结果:

{1=Java, 2=数据库, 3=Vue}
key = 1, value = Java
key = 2, value = 数据库
key = 3, value = Vue

方式四 通过For-Each迭代entries,使用Map.entrySet遍历

@Test
public void testHashMap4() {
 Map<Integer, String> map = new HashMap<>();
 map.put(001, "Java");
 map.put(002, "数据库");
 map.put(003, "Vue");
 System.out.println(map);

 // 使用For-Each迭代entries,通过Map.entrySet遍历key和value
 for (Map.Entry<Integer, String> entry : map.entrySet()) {
 System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
 }
}

{1=Java, 2=数据库, 3=Vue}
key = 1, value = Java
key = 2, value = 数据库
key = 3, value = Vue

方式五 使用lambda表达式forEach遍历

@Test
public void testHashMap5() {
 Map<Integer, String> map = new HashMap<>();
 map.put(001, "Java");
 map.put(002, "数据库");
 map.put(003, "Vue");
 System.out.println(map);

	// 使用lambda表达式forEach遍历
 map.forEach((k, v) -> System.out.println("key = " + k + ", value = " + v));
}

forEach 源码

default void forEach(BiConsumer<? super K, ? super V> action) {
 Objects.requireNonNull(action);
 for (Map.Entry<K, V> entry : entrySet()) {
  K k;
  V v;
  try {
  k = entry.getKey();
  v = entry.getValue();
  } catch(IllegalStateException ise) {
  // this usually means the entry is no longer in the map.
  throw new ConcurrentModificationException(ise);
  }
  action.accept(k, v);
 }
 }

从源码可以看到,这种新特性就是在传统的迭代方式上加了一层壳,但是让代码变得更加简单。(开发中推荐使用)

总结

推荐使用 entrySet 遍历 Map 类集合 KV (文章中的第四种方式),而不是 keySet 方式进行遍历。

keySet 其实是遍历了 2 次,第一次是转为 Iterator 对象,第二次是从 hashMap 中取出 key 所对应的 value值。而 entrySet 只是遍历了一次,就把 key 和 value 都放到了 entry 中,效率更高。

values()返回的是 V 值集合,是一个 list 集合对象;keySet()返回的是 K 值集合,是一个 Set 集合对象;entrySet()返回的是 K-V 值组合集合。

如果是 JDK8,推荐使用Map.forEach 方法(文章中的第五种方式)。

感谢你能够认真阅读完这篇文章,希望小编分享的“Java中遍历Map集合的方法有哪些”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI