遍历Map集合的四种方式
来源:互联网 发布:小米note3网络设置 编辑:程序博客网 时间:2024/05/29 14:44
1. 在for-each循环中使用entries来遍历
Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> entry : map.entrySet()) { System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); }
2.在for-each循环中遍历keys或values
如果只需要map中的键或者值,你可以通过keySet或values来实现遍历,而不是用entrySet
//遍历map中的键 for (Integer key : map.keySet()) { System.out.println("Key = " + key); } //遍历map中的值for (Integer value : map.values()) { System.out.println("Value = " + value);}
3.使用Iterator遍历
Map<Integer, Integer> map = new HashMap<Integer, Integer>(); Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator(); while (entries.hasNext()) { Map.Entry<Integer, Integer> entry = entries.next(); System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); }
4.通过键找值遍历(效率低)
Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (Integer key : map.keySet()) { Integer value = map.get(key); System.out.println("Key = " + key + ", Value = " + value); }
阅读全文
0 0
- 四种Map集合的遍历方式
- 遍历Map集合的四种方式
- Map集合遍历的四种方式
- Map集合的四种遍历方式
- Map集合的四种遍历方式
- Map集合的四种遍历方式
- Map集合的四种遍历方式
- Map集合的四种遍历方式
- Map集合的四种遍历方式
- Map集合的四种遍历方式
- Map集合的四种遍历方式
- Map集合的四种遍历方式
- Map集合的四种遍历方式
- Map集合的四种遍历方式
- Map集合的四种遍历方式
- Map集合的四种遍历方式
- map集合的四种遍历方式
- Map集合的四种遍历方式
- 20.调用系统浏览器打开网页-隐式Intent的用法
- 终于开通了,以后这里将会记录我学习中的点点滴滴
- YOLO工程代码如何在windows上配置和运行!window上YOLO训练样本的制作
- 注册表获取系统驱动版本信息
- Android AsyncTask几个注意事项
- 遍历Map集合的四种方式
- 27_键盘输入_Scanner类的使用_import简单入门
- TNS-12535 TNS-00505
- XML教程进阶05_相关技术
- org.apache.commons.lang.StringUtils 将数组变成字符串
- 高考作文做个标题党,有何不好!
- 练习65
- 统计数组中相同的某一元素的其他 指定元素总和
- led-class.c的代码逻辑