本文共 4350 字,大约阅读时间需要 14 分钟。
HashMap 遍历从大的方向来说,可分为以下 4 类:
迭代器(Iterator)方式遍历;
For Each 方式遍历; Lambda 表达式遍历(JDK 1.8+); Streams API 遍历(JDK 1.8+)。 但每种类型下又有不同的实现方式,因此具体的遍历方式又可以分为以下 7 种:使用迭代器(Iterator)EntrySet 的方式进行遍历;
使用迭代器(Iterator)KeySet 的方式进行遍历; 使用 For Each EntrySet 的方式进行遍历; 使用 For Each KeySet 的方式进行遍历; 使用 Lambda 表达式的方式进行遍历; 使用 Streams API 单线程的方式进行遍历; 使用 Streams API 多线程的方式进行遍历。//ForEach EntrySet for (Map.Entryme :resultMap.entrySet()){ System.out.println("EntrySet "+me.getKey() +" = "+ me.getValue()); } //ForEach keySet for (String key : resultMap.keySet()){ System.out.println("keySet "+key +" = "+ resultMap.get(key)); } //Lambda forEach resultMap.forEach((key,value) -> { System.out.println("Lambda forEach " + key +" = "+value); }); //Streams API 单线程 resultMap.entrySet().stream().forEach((entry)->{ System.out.println("Streams API 单线程 " + entry.getKey() +" = "+entry.getValue()); }); //Streams API 多线程 resultMap.entrySet().parallelStream().forEach((entry)->{ System.out.println("Streams API 多线程 " + entry.getKey() +" = "+entry.getValue()); });
public class HashMapTest { public static void main(String[] args) { HashMapmap = new HashMap<>(); map.put(1, "aaa"); map.put(2, "abb"); map.put(3, "acc"); // System.out.println(map.get(1)); //方式1 键找值 Set keySet() //1.集合 { 1=aaa, 2=abb, 3=acc} System.out.println(map); //2.通过values 获取所有值,不能获取到key对象 for (Integer i : map.keySet()) { System.out.println(map.get(i)); } // 第一种方式: 使用keySet // 需要分别获取key和value,没有面向对象的思想 Set ks = map.keySet(); /* Iterator it = ks.iterator(); while (it.hasNext()) { Integer key = it.next(); String value = map.get(key); System.out.println("key=" + key + " value=" + value); }*/ for(Integer key : ks){ String value = map.get(key); System.out.println(key+"---"+value); } System.out.println(); //方式2 键值对对象找键和值 Map.Entry对象 推荐使用 //Set > entrySet() /* Set > es = map.entrySet(); Iterator > it2 = es.iterator(); while(it2.hasNext()){ // 返回的是封装了key和value对象的Map.Entry对象 Map.Entry en = it2.next(); // 获取Map.Entry对象中封装的key和value对象 Integer key = en.getKey(); String value = en.getValue(); System.out.println("key2=" + key + " value2=" + value); }*/ //Set > entries = map.entrySet(); Set > set2 = map.entrySet(); for (Map.Entry me : set2) { Integer key = me.getKey(); String value = me.getValue(); System.out.println(key + "---" + value); } }}
//方式2 键值对对象找键和值 Map.Entry对象 推荐使用
最终代码public class HashMapTest { public static void main(String[] args) { HashMapmap = new HashMap<>(); map.put(1, "aaa"); map.put(2, "abb"); map.put(3, "acc"); // System.out.println(map.get(1)); //方式1 键找值 Set keySet() //1.集合 { 1=aaa, 2=abb, 3=acc} System.out.println(map); //2.通过values 获取所有值,不能获取到key对象 for (Integer i : map.keySet()) { System.out.println(map.get(i)); } // 第一种方式: 使用keySet // 需要分别获取key和value,没有面向对象的思想 Set ks = map.keySet(); for (Integer key : ks) { String value = map.get(key); System.out.println(key + "---" + value); } System.out.println(); //方式2 键值对对象找键和值 Map.Entry对象 推荐使用 //Set > entrySet() // 返回的是封装了key和value对象的Map.Entry对象 for (Map.Entry me : map.entrySet()) { // 获取Map.Entry对象中封装的key和value对象 Integer key = me.getKey(); String value = me.getValue(); System.out.println(key + "---" + value); } }}
HashMapmap = new HashMap<>(); map.put(1, "aaa"); map.put(2, "abb"); map.put(3, "acc");// 传统的Map迭代方式 for (Map.Entry entry : map.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); }// JDK8的迭代方式 map.forEach((key, value) -> { System.out.println(key + ":" + value); });
转载地址:http://dtxab.baihongyu.com/