目录
3.1同步既排队 线程安全的 hashtable 键不可以为null,值也不能为null 3.2异步 非安全的 hashmap 键可以为null,值也可以为null
4.1 Collections:工具类,提供一组静态方法操作Collection集合 4.2 Arrays:工具类,提供了一组静态方法操作数组
5.1需要的集合List Map Map,map>,list>
1.思维导图集合框架(collection)
2. Map的特点与遍历方式
2.1 特点:无序、以键值对的形式添加元素,键不能重复,值可以重复
它没有继承Collection接口
2.2 遍历
2.2.1 先取出保存所有键的Set,再遍历Set即可(2种)
2.2.2 先取出保存所有Entry的Set,再遍历此Set即可 (重要点)
2.3代码部分//map集合的特点 无序,键值对,键不可以重复,值可以重复 Map<String, Object> map = new HashMap<String,Object>(); map.put("aa", "小明"); map.put("bb", "小玲"); map.put("cc", "小李"); map.put("dd", "小黄"); //map集合的遍历方式1.获取map集合中的所有key的set集合,再通过键获得值 Set<String> keyset = map.keySet(); for (String key : keyset) { // System.out.println(key); //通过键获取对应的值 Object value = map.get(key); System.out.println(key+"="+value); } map.put("dd", "小黄1"); map.put("dd1", "小黄"); System.out.println("--------------"); //map遍历方式2.先取出保存所有键值对(Entry)的set,再遍历此set Set<Entry<String, Object>> entrySet = map.entrySet(); for (Entry<String, Object> entry : entrySet) { System.out.println(entry); }
2.4遍历结果截图
3.HashMap与HashTable之间的区别
3.1同步既排队 线程安全的 hashtable 键不可以为null,值也不能为null
3.2异步 非安全的 hashmap 键可以为null,值也可以为null
4. 其它
4.1 Collections:工具类,提供一组静态方法操作Collection集合
4.2 Arrays:工具类,提供了一组静态方法操作数组
5.小案例Map集合3层嵌套使用(2Map+1List)
5.1需要的集合List<Student> Map<String,List> Map<String,Map>
5.2需要一个student
5.3代码部分
5.3.1student实体类
package com.zking.zy; import java.io.Serializable; public class Student implements Serializable{ private static final long serialVersionUID = -6955357754496510375L; 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) { this.name = name; this.score = score; } @Override public String toString() { return "Student [name=" + name + ", score=" + score + "]"; } }
5.3.2测试类
package com.zking.zy; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class Zy04 { public static void main(String[] args) { List<Student> ls = new ArrayList<Student>(); List<Student> ls1 = new ArrayList<Student>(); List<Student> ls2 = new ArrayList<Student>(); List<Student> ls3 = new ArrayList<Student>(); List<Student> ls4 = new ArrayList<Student>(); List<Student> ls5 = new ArrayList<Student>(); Map<String, List<Student>> map = new HashMap<String, List<Student>>(); Map<String, List<Student>> mapa = new HashMap<String, List<Student>>(); Map<String, List<Student>> mapb = new HashMap<String, List<Student>>(); Map<String, Map<String,List<Student>>> map1 = new HashMap<String, Map<String,List<Student>>>(); map1.put("卓京初中部", map); map.put("T110", ls); ls.add(new Student("张晓东", 90)); ls.add(new Student("张晓西", 75)); map.put("T111", ls1); ls1.add(new Student("张晓北", 80)); ls1.add(new Student("张晓南", 82)); map1.put("卓京高中部", mapa); mapa.put("T222", ls2); ls2.add(new Student("张三", 90)); ls2.add(new Student("李四", 100)); mapa.put("T206", ls3); ls3.add(new Student("王五", 70)); ls3.add(new Student("小六", 100)); map1.put("卓京大学部", mapb); mapb.put("T230", ls4); ls4.add(new Student("可乐", 60)); ls4.add(new Student("雪碧", 50)); mapb.put("T231", ls5); ls5.add(new Student(" 哇哈哈", 90)); ls5.add(new Student("老干妈", 80)); Set<String> keySet = map1.keySet(); Set<String> keySet2 = map.keySet(); for (String key : keySet) { System.out.println(key); Map<String, List<Student>> map2 = map1.get(key); Set<Entry<String,List<Student>>> entrySet = map2.entrySet(); for (Entry<String, List<Student>> entry : entrySet) { System.out.println("\t"+entry.getKey()); List<Student> value = entry.getValue(); for (Student student : value) { System.out.println("\t"+"\t"+student.getName()+" "+student.getScore()); } } } } }
5.3控制台打印效果截图