1:Map(掌握) (1)将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。 (2)Map和Collection的区别? A:Map 存储的是键值对形式的元素,键唯一,值可以重复。夫妻对 B:Collection 存储的是单独出现的元素,子接口Set元素唯一,子接口List元素可重复。光棍 (3)Map接口功能概述(自己补齐) A:添加功能 B:删除功能 C:判断功能 D:获取功能 E:长度功能
* Map集合的特点: * 将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。 * * Map集合和Collection集合的区别? * Map集合存储元素是成对出现的,Map集合的键是唯一的,值是可重复的。可以把这个理解为:夫妻对 * Collection集合存储元素是单独出现的,Collection的儿子Set是唯一的,List是可重复的。可以把这个理解为:光棍(11.11) * * 注意: * Map集合的数据结构值针对键有效,跟值无关 * HashMap,TreeMap等会讲。 * Collection集合的数据结构是针对元素有效 * * Map集合的功能概述: * 1:添加功能 * V put(K key,V value):添加元素。这个其实还有另一个功能?先不告诉你,等会讲 * 如果键是第一次存储,就直接存储元素,返回null * 如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值 * 2:删除功能 * void clear():移除所有的键值对元素 * V remove(Object key):根据键删除键值对元素,并把值返回 * 3:判断功能 * boolean containsKey(Object key):判断集合是否包含指定的键 * boolean containsValue(Object value):判断集合是否包含指定的值 * boolean isEmpty():判断集合是否为空 * 4:获取功能 * Set<Map.Entry<K,V>> entrySet():??? * V get(Object key):根据键获取值 * Set<K> keySet():获取集合中所有键的集合 * Collection<V> values():获取集合中所有值的集合 * 5:长度功能 * int size():返回集合中的键值对的对数 */public class MapDemo { public static void main(String[] args) { // 创建集合对象 Map<String, String> map = new HashMap<String, String>(); // 添加元素 // V put(K key,V value):添加元素。这个其实还有另一个功能?先不告诉你,等会讲 // System.out.PRintln("put:" + map.put("文章", "马伊俐")); // System.out.println("put:" + map.put("文章", "姚笛")); map.put("邓超", "孙俪"); map.put("黄晓明", "杨颖"); map.put("周杰伦", "蔡依林"); map.put("刘恺威", "杨幂"); // void clear():移除所有的键值对元素 // map.clear(); // V remove(Object key):根据键删除键值对元素,并把值返回 // System.out.println("remove:" + map.remove("黄晓明")); // System.out.println("remove:" + map.remove("黄晓波")); // boolean containsKey(Object key):判断集合是否包含指定的键 // System.out.println("containsKey:" + map.containsKey("黄晓明")); // System.out.println("containsKey:" + map.containsKey("黄晓波")); // boolean isEmpty():判断集合是否为空 // System.out.println("isEmpty:"+map.isEmpty()); //int size():返回集合中的键值对的对数 System.out.println("size:"+map.size()); // 输出集合名称 System.out.println("map:" + map); }}(4)Map集合的遍历 A:键找值 a:获取所有键的集合 b:遍历键的集合,得到每一个键 c:根据键到集合中去找值/* * Map集合的遍历。 * Map -- 夫妻对 * 思路: * A:把所有的丈夫给集中起来。 * B:遍历丈夫的集合,获取得到每一个丈夫。 * C:让丈夫去找自己的妻子。 * * 转换: * A:获取所有的键 * B:遍历键的集合,获取得到每一个键 * C:根据键去找值 */public class MapDemo3 { public static void main(String[] args) { // 创建集合对象 Map<String, String> map = new HashMap<String, String>(); // 创建元素并添加到集合 map.put("杨过", "小龙女"); map.put("郭靖", "黄蓉"); map.put("杨康", "穆念慈"); map.put("陈玄风", "梅超风"); // 遍历 // 获取所有的键 Set<String> set = map.keySet(); // 遍历键的集合,获取得到每一个键 for (String key : set) { // 根据键去找值 String value = map.get(key); System.out.println(key + "---" + value); } }} B:键值对对象找键和值 a:获取所有的键值对对象的集合 b:遍历键值对对象的集合,获取每一个键值对对象 c:根据键值对对象去获取键和值 代码体现: Map<String,String> hm = new HashMap<String,String>(); hm.put("it002","hello"); hm.put("it003","world"); hm.put("it001","java"); //方式1 键找值 Set<String> set = hm.keySet(); for(String key : set) { String value = hm.get(key); System.out.println(key+"---"+value); } //方式2 键值对对象找键和值 Set<Map.Entry<String,String>> set2 = hm.entrySet(); for(Map.Entry<String,String> me : set2) { String key = me.getKey(); String value = me.getValue(); System.out.println(key+"---"+value); } (5)HashMap集合的练习 A:HashMap<String,String>/* * TreeMap:是基于红黑树的Map接口的实现。 * * HashMap<String,String> * 键:String * 值:String */public class TreeMapDemo { public static void main(String[] args) { // 创建集合对象 TreeMap<String, String> tm = new TreeMap<String, String>(); // 创建元素并添加元素 tm.put("hello", "你好"); tm.put("world", "世界"); tm.put("java", "爪哇"); tm.put("world", "世界2"); tm.put("javaee", "爪哇EE"); // 遍历集合 Set<String> set = tm.keySet(); for (String key : set) { String value = tm.get(key); System.out.println(key + "---" + value); } }} B:HashMap<Integer,String> C:HashMap<String,Student> D:HashMap<Student,String>/* * HashMap<Student,String> * 键:Student * 要求:如果两个对象的成员变量值都相同,则为同一个对象。 * 值:String */public class HashMapDemo4 { public static void main(String[] args) { // 创建集合对象 HashMap<Student, String> hm = new HashMap<Student, String>(); // 创建学生对象 Student s1 = new Student("貂蝉", 27); Student s2 = new Student("王昭君", 30); Student s3 = new Student("西施", 33); Student s4 = new Student("杨玉环", 35); Student s5 = new Student("貂蝉", 27); // 添加元素 hm.put(s1, "8888"); hm.put(s2, "6666"); hm.put(s3, "5555"); hm.put(s4, "7777"); hm.put(s5, "9999"); // 遍历 Set<Student> set = hm.keySet(); for (Student key : set) { String value = hm.get(key); System.out.println(key.getName() + "---" + key.getAge() + "---" + value); } }}(6)TreeMap集合的练习 A:TreeMap<String,String>/* * TreeMap:是基于红黑树的Map接口的实现。 * * HashMap<String,String> * 键:String * 值:String */public class TreeMapDemo { public static void main(String[] args) { // 创建集合对象 TreeMap<String, String> tm = new TreeMap<String, String>(); // 创建元素并添加元素 tm.put("hello", "你好"); tm.put("world", "世界"); tm.put("java", "爪哇"); tm.put("world", "世界2"); tm.put("javaee", "爪哇EE"); // 遍历集合 Set<String> set = tm.keySet(); for (String key : set) { String value = tm.get(key); System.out.println(key + "---" + value); } }} B:TreeMap<Student,String>/* * TreeMap<Student,String> * 键:Student * 值:String */public class TreeMapDemo2 { public static void main(String[] args) { // 创建集合对象 TreeMap<Student, String> tm = new TreeMap<Student, String>( new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { // 主要条件 int num = s1.getAge() - s2.getAge(); // 次要条件 int num2 = num == 0 ? s1.getName().compareTo( s2.getName()) : num; return num2; } }); // 创建学生对象 Student s1 = new Student("潘安", 30); Student s2 = new Student("柳下惠", 35); Student s3 = new Student("唐伯虎", 33); Student s4 = new Student("燕青", 32); Student s5 = new Student("唐伯虎", 33); // 存储元素 tm.put(s1, "宋朝"); tm.put(s2, "元朝"); tm.put(s3, "明朝"); tm.put(s4, "清朝"); tm.put(s5, "汉朝"); // 遍历 Set<Student> set = tm.keySet(); for (Student key : set) { String value = tm.get(key); System.out.println(key.getName() + "---" + key.getAge() + "---" + value); } }}(7)案例 A:统计一个字符串中每个字符出现的次数 B:集合的嵌套遍历 a:HashMap嵌套HashMap b:HashMap嵌套ArrayList c:ArrayList嵌套HashMap d:多层嵌套2:Collections(理解) (1)是针对集合进行操作的工具类 (2)面试题:Collection和Collections的区别 A:Collection 是单列集合的顶层接口,有两个子接口List和Set B:Collections 是针对集合进行操作的工具类,可以对集合进行排序和查找等
/* * Collections:是针对集合进行操作的工具类,都是静态方法。 * * 面试题: * Collection和Collections的区别? * Collection:是单列集合的顶层接口,有子接口List和Set。 * Collections:是针对集合操作的工具类,有对集合进行排序和二分查找的方法 * * 要知道的方法 * public static <T> void sort(List<T> list):排序 默认情况下是自然顺序。 * public static <T> int binarySearch(List<?> list,T key):二分查找 * public static <T> T max(Collection<?> coll):最大值 * public static void reverse(List<?> list):反转 * public static void shuffle(List<?> list):随机置换 */public class CollectionsDemo { public static void main(String[] args) { // 创建集合对象 List<Integer> list = new ArrayList<Integer>(); // 添加元素 list.add(30); list.add(20); list.add(50); list.add(10); list.add(40); System.out.println("list:" + list); // public static <T> void sort(List<T> list):排序 默认情况下是自然顺序。 // Collections.sort(list); // System.out.println("list:" + list); // [10, 20, 30, 40, 50] // public static <T> int binarySearch(List<?> list,T key):二分查找 // System.out // .println("binarySearch:" + Collections.binarySearch(list, 30)); // System.out.println("binarySearch:" // + Collections.binarySearch(list, 300)); // public static <T> T max(Collection<?> coll):最大值 // System.out.println("max:"+Collections.max(list)); // public static void reverse(List<?> list):反转 // Collections.reverse(list); // System.out.println("list:" + list); //public static void shuffle(List<?> list):随机置换 Collections.shuffle(list); System.out.println("list:" + list); }}(3)常见的几个小方法: A:public static <T> void sort(List<T> list) B:public static <T> int binarySearch(List<?> list,T key) C:public static <T> T max(Collection<?> coll) D:public static void reverse(List<?> list) E:public static void shuffle(List<?> list)(4)案例 A:ArrayList集合存储自定义对象的排序 B:模拟斗地主洗牌和发牌 C:模拟斗地主洗牌和发牌并对牌进行排序新闻热点
疑难解答