【Java集合的详细研究4】Java中如何遍历Map对象的4种方法
来源:互联网 发布:pc端淘宝首页不见了 编辑:程序博客网 时间:2024/06/15 18:18
方法一 通过Map.entrySet遍历key和value,在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()); }
方法二、通过Map.keySet遍历key,通过键找值value遍历(效率低),普遍使用,二次取值
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); }
方法三 如果只需要map中的键或者值,你可以通过Map.keySet或Map.values来实现遍历,而不是用entrySet。在for-each循环中遍历keys或values。
Map<Integer, Integer> map = new HashMap<Integer, Integer>(); //遍历map中的键 for (Integer key : map.keySet()) { System.out.println("Key = " + key); } //遍历map中的值 for (Integer value : map.values()) { System.out.println("Value = " + value); }
方法四:通过Map.entrySet使用iterator遍历key和value
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()); }
全部代码:
public static void main(String[] args) { init(); traversal(); } // HashMap按照哈希算法来存取键对象,有很好的存取性能 private static HashMap<String, String> hMap = new HashMap<>(); // TreeMap实现了SortedMap接口,能对键对象进行排序。支持自然排序和客户化排序两种方式。 private static TreeMap<String, String> tMap = new TreeMap<>(); // map的赋值 public static void init() { hMap.put("1", "value1"); hMap.put("2", "value2"); hMap.put("3", "value3"); } // map的遍历 public static void traversal() { // 第一种:普遍使用,二次取值 System.out.println("通过Map.keySet遍历key和value:"); for (String key : hMap.keySet()) { System.out.println("key= " + key + " and value= " + hMap.get(key)); } // 第二种 System.out.println("通过Map.entrySet使用iterator遍历key和value:"); Iterator<Map.Entry<String, String>> it = hMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, String> entry = it.next(); System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); } // 第三种:推荐,尤其是容量大时 System.out.println("通过Map.entrySet遍历key和value"); for (Map.Entry<String, String> entry : hMap.entrySet()) { System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); } // 第四种 System.out.println("通过Map.values()遍历所有的value,但不能遍历key"); for (String v : hMap.values()) { System.out.println("value= " + v); } }
运行结果:
通过Map.keySet遍历key和value:
key= 1 and value= value1
key= 2 and value= value2
key= 3 and value= value3
通过Map.entrySet使用iterator遍历key和value:
key= 1 and value= value1
key= 2 and value= value2
key= 3 and value= value3
通过Map.entrySet遍历key和value
key= 1 and value= value1
key= 2 and value= value2
key= 3 and value= value3
通过Map.values()遍历所有的value,但不能遍历key
value= value1
value= value2
value= value3
0 0
- 【Java集合的详细研究4】Java中如何遍历Map对象的4种方法
- 【Java集合的详细研究4】Java中如何遍历Map对象的4种方法
- Java中如何遍历Map对象的4种方法
- Java中如何遍历Map对象的4种方法
- Java中如何遍历Map对象的4种方法
- Java中如何遍历Map对象的4种方法
- Java中如何遍历Map对象的4种方法
- Java中如何遍历Map对象的4种方法
- Java中如何遍历Map对象的4种方法
- Java中如何遍历Map对象的4种方法
- Java中如何遍历Map对象的4种方法
- Java中如何遍历Map对象的4种方法
- Java中如何遍历Map对象的4种方法
- Java中如何遍历Map对象的4种方法
- Java中如何遍历Map对象的4种方法
- Java中如何遍历Map对象的4种方法
- Java中如何遍历Map对象的4种方法
- Java中如何遍历Map对象的4种方法
- 如何确认手势所在的UI的tag值/以及 can not find file path
- 【架构师之路】-【01设计模式】-03设计原则之里式替换原则
- JS判断元素是否在数组内
- 面试内容
- 样本估计中方差用m-1代替m的理解
- 【Java集合的详细研究4】Java中如何遍历Map对象的4种方法
- Windows 10 运行 Linux子系统的图形界面程序
- Eclipse不能不说的秘密:远程Debug
- JAVA多线程和并发基础面试问答(转载)
- elasticsearch and or
- 备份git 仓库 .bat脚本
- HTTP Content-type类型学习
- nginx配置多站点+git常用命令
- Spring Boot任务调度简单入门