一、Map集合的特点
Map(不继承Collection接口) 特点:无序,以键值对的形式存储数据,键不可以重复(Set),值可以重复(List)
二、Map集合俩种遍历方式
2.1Map集合遍历方式 1.获取map集合中的所有key的set集合,在通过键得到对应的值
Map<String,Object> m = new HashMap<String,Object>(); m.put("aa","a"); m.put("bb","b"); m.put("cc","c"); // Map集合遍历方式 1.获取map集合中的所有key的set集合,在通过键得到对应的值 Set<String> a = m.keySet();//拿到所有键 for (String string : a) { //m.get(string);根据键获取对应的值 System.out.println(string+"="+m.get(string)); } Collection<Object> values = m.values();//拿到所有值 for (Object object : values) { System.out.println(object); } System.out.println("~~~~~~~~~~~~~~"); //键不能重复 m.put("cc","c1"); for (String string : a) { //m.get(string);根据键获取对应的值 System.out.println(string+"="+m.get(string)); }
运行效果图:
2.2,Map遍历方式2:先取出保存所有键值数(Entry的set,在遍历此set)
代码:
Set<Entry<String,Object>> entrySet = m.entrySet(); Map<String,Object> m = new HashMap<String,Object>(); m.put("aa","a"); m.put("bb","b"); m.put("cc","c"); for (Entry<String, Object> entry : entrySet) { System.out.println(entry); System.out.println(entry.getKey());//拿到所有键 System.out.println(entry.getValue());//拿到所有值 }
运行效果图:
三、HashMap与HashTable之间的区别
hashtable: 同步既排队 线程安全的 键不可以为null,值也不能为null
hashmap :异步 非安全的 键可以为null,值也可以为null测试Hashtable:
代码:
package com.zking.map; import java.util.Collection; import java.util.HashMap; import java.util.Hashtable; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class Demo2 { public static void main(String[] args) { //Map(不继承Collection接口) 特点:无序,以键值对的形式存储数据,键不可以重复(Set),值可以重复(List) //定义一个HashMap集合 Map<String,Object> m = new HashMap<String,Object>(); //定义一个Hashtable集合 Map<String,Object> m1 = new Hashtable<>(); //往Hashtable中加空值 m1.put("a","lx"); m1.put(null,null); m1.put(null,"xl"); Set<Entry<String,Object>> entrySet = m1.entrySet(); for (Entry<String, Object> entry : entrySet) { System.out.println(entry); } } }
运行效果图:
从效果图可以看出报空指针异常由此Hashtable中键值不能为空
测试HashMap:
代码:
package com.zking.map; import java.util.Collection; import java.util.HashMap; import java.util.Hashtable; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class Demo2 { public static void main(String[] args) { //Map(不继承Collection接口) 特点:无序,以键值对的形式存储数据,键不可以重复(Set),值可以重复(List) //定义一个HashMap集合 Map<String,Object> m = new HashMap<String,Object>(); //定义一个HashMap集合 Map<String,Object> m1 = new Hashtable<>(); m.put("a","lx"); m.put(null,null); m.put(null,"xl"); Set<Entry<String,Object>> entrySet = m.entrySet(); for (Entry<String, Object> entry : entrySet) { System.out.println(entry); } } }
运行效果图:
从效果图可以看出报空指针异常由此HashMap中键值可以为空
四、Map集合3层嵌套使用(2Map+1List)
代码实体类:
package com.zking.map; import java.io.Serializable; public class Student implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private String name; private int score; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public Student() { // TODO Auto-generated constructor stub } public Student(String name, int score) { super(); this.name = name; this.score = score; } @Override public String toString() { return "Student [name=" + name + ", score=" + score + "]"; } }
主类:
package com.zking.map; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class Demo5 { public static void main(String[] args) { //四.Map集合3层嵌套使用(2Map+1List) Map<String,Map<String,List<Student>>> map = new HashMap<>(); List<Student> list = new ArrayList<Student>(); list.add(new Student("张晓东",90)); list.add(new Student("张晓西",75)); List<Student> list1 = new ArrayList<Student>(); list1.add(new Student("张晓北",80)); list1.add(new Student("张晓南",82)); //初中部 Map<String,List<Student>> map1 = new HashMap<>(); map1.put("T110",list); map1.put("T111",list1); //高中部 Map<String,List<Student>> map2 = new HashMap<>(); List<Student> list2 = new ArrayList<Student>(); list2.add(new Student("张三",90)); list2.add(new Student("李四",100)); List<Student> list3 = new ArrayList<Student>(); list3.add(new Student("王五",70)); list3.add(new Student("小六",100)); List<Student> list4 = new ArrayList<Student>(); list4.add(new Student("小八",70)); list4.add(new Student("小仇",100)); map2.put("T206",list2); map2.put("T222",list3); map2.put("T208",list4); //大学部 Map<String,List<Student>> map3 = new HashMap<>(); List<Student> list5 = new ArrayList<Student>(); list5.add(new Student("可乐",90)); list5.add(new Student("雪碧",100)); List<Student> list6 = new ArrayList<Student>(); list6.add(new Student("王哈哈",90)); list6.add(new Student("老干妈",80)); map3.put("T230",list5); map3.put("T231",list6); map.put("卓京初中部",map1); map.put("卓京高中部",map2); map.put("卓京大学部",map3); //先拿到最大层的所有键值对 Set<Entry<String,Map<String,List<Student>>>> entrySet = map.entrySet(); for (Entry<String, Map<String, List<Student>>> entry : entrySet) { //拿到最外层的键 System.out.println(entry.getKey()); //拿到值 Map<String, List<Student>> value = entry.getValue(); //拿到值Map的键值对 Set<Entry<String, List<Student>>> entrySet2 = value.entrySet(); for (Entry<String, List<Student>> entry2 : entrySet2) { //拿到键 System.out.println("\t"+entry2.getKey()); //拿到值 返回Student集合 List<Student> value2 = entry2.getValue(); for(Student ls:value2) { System.out.println("\t"+"\t"+ls.getName()+" "+ls.getScore()); } } } } }
运行效果:
思路:首先我们看到运行效果图最外层的是卓京什么部然后它下面还有对应的值,然后值下面还有对应的键和值,所以我们值里面还要放一个Map集合,然后关于Map里面值的Map集合里面的键和值,键就是对应班级,然后可以看效果图值是一个List集合里面放的是Student学生对象,然后最外层的Map集合所对应的值(Map集合)都是不一样的,所以我们每加一个键都要加对应的值(new一个新的Map集合),最后就是加里层Map集合的值(Map集合中的值)就是list集合实例化加数据就行了。完了之后就是层层遍历出来