遍历Map集合
1、遍历普通的HashMap集合
-
方法1:通过
keySet()
方法,获取Map集合的键集合Set,通过键集合遍历键值 -
方法2:通过
entrySet()
方法,同时获取键值对,然后再遍历键值 -
实例
package advanced.seven; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * @author Object * @description Map集合 * @date 2020/9/10 */ public class Demo01 { public static void main(String[] args) { Map<String,Student> map1 = new HashMap<>(); map1.put("001",new Student("张三",19)); map1.put("002",new Student("李四",19)); map1.put("003",new Student("张三封",25)); map1.put("004",new Student("王五",24)); Set<String> keys1 = map1.keySet(); for (String key:keys1 ) { System.out.println(key+"<====>"+map1.get(key).getName()+map1.get(key).getAge()); } System.out.println("=========="); Set<Map.Entry<String, Student>> entries1 = map1.entrySet(); for (Map.Entry<String, Student> entry:entries1 ) { System.out.println(entry.getKey()+"<====>"+entry.getValue().getName()+entry.getValue().getAge()); } System.out.println("=============="); Map<String,String> map = new HashMap<>(); map.put("001","张三"); map.put("002","李四"); map.put("003","张三"); map.put("004","王五"); map.put("005","赵六"); map.put("006","田七"); //获取键集合,通过键遍历map集合 Set<String> keys = map.keySet(); for (String key:keys ) { System.out.println(key+"<===>"+map.get(key)); } System.out.println("==========="); //获取键值对,通过键值对遍历map集合 Set<Map.Entry<String, String>> entries = map.entrySet(); for (Map.Entry<String, String> entry:entries ) { System.out.println(entry.getKey()+"<===>"+entry.getValue()); } } }
2、HashMap嵌套ArrayList的遍历
-
方法1:通过
keySet()
方法,获取Map集合的键集合Set,通过键集合遍历键值,然后对值进行遍历 -
方法2:通过
entrySet()
方法,同时获取键值对,然后再遍历键值 -
实例
package advanced.seven; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * @author Object * @description HashMap集合里面放ArrayList * @date 2020/9/10 */ public class Demo03 { public static void main(String[] args) { HashMap<String, ArrayList<Student>> hm = new HashMap<>(); ArrayList<Student> arr1 = new ArrayList<>(); arr1.add(new Student("刘德华",18)); arr1.add(new Student("梁朝伟",23)); ArrayList<Student> arr2 = new ArrayList<>(); arr2.add(new Student("郭德纲",18)); arr2.add(new Student("于谦",23)); ArrayList<Student> arr3 = new ArrayList<>(); arr3.add(new Student("刘翔",18)); arr3.add(new Student("林丹",23)); hm.put("学神",arr1); hm.put("学霸",arr2); hm.put("学渣",arr3); //获取键集合 Set<String> keySet = hm.keySet(); for (String key:keySet ) { System.out.println(key+"========"); //遍历每个键的值 for (Student name: hm.get(key)) { System.out.println("\t"+name.getName()); } } System.out.println("============="); //获取键值对集合 Set<Map.Entry<String, ArrayList<Student>>> entrySet = hm.entrySet(); //遍历键值对集合 for (Map.Entry<String, ArrayList<Student>> entry: entrySet ) { System.out.println(entry.getKey()+"======="); //遍历每个键的值 for (Student name:entry.getValue() ) { System.out.println("\t"+name.getName()); } } } }
3、ArrayList嵌套HashMap的遍历
-
实例
package advanced.seven; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * @author Object * @description ArrayList集合中包含HashMap * @date 2020/9/10 */ public class Demo2 { public static void main(String[] args) { ArrayList<Map<String, Student>> maps = new ArrayList<>(); Map<String,Student> map1 = new HashMap<>(); map1.put("001",new Student("张三",19)); map1.put("002",new Student("李四",19)); Map<String,Student> map2 = new HashMap<>(); map2.put("003",new Student("张三封",25)); Map<String,Student> map3 = new HashMap<>(); map3.put("004",new Student("王五",24)); maps.add(map1); maps.add(map2); maps.add(map3); //遍历list集合 for (Map<String, Student> map:maps) { //获取map集合的键集合 Set<String> strings = map.keySet(); //根据键集合遍历键值 for (String s:strings ) { System.out.println(s + map.get(s).getName()); } } System.out.println("============"); //遍历list集合 for (Map<String, Student> map:maps ) { //获取map的键值对集合 Set<Map.Entry<String, Student>> entries = map.entrySet(); // 遍历键值对集合 for (Map.Entry<String, Student> entry:entries ) { System.out.println(entry.getKey()+entry.getValue().getName()); } } } }