1、Map(掌握)
(1)将键映射到值的对象。一个映射不能包含重复的键:每个键最多只能映射到一个值。
(2)Map和Collection的区别?
A: Map 存储的是键值对形式的元素,键唯一,值可以重复。 理解为:夫妻对
B: Collection存储的是单独出现的元素,子接口List元素可重复,子接口Set元素唯一。 理解为: 光棍
(3)Map接口功能概述
A: 添加功能
V put(K key, V value) 将指定的值与该映射中的指定键相关联(可选操作)。
B: 删除功能
void clear() 从该地图中删除所有的映射(可选操作)。 V remove(Object key) 如果存在(从可选的操作),从该地图中删除一个键的映射。
C: 判断功能
boolean containsKey(Object key) 如果此映射包含指定键的映射,则返回 true 。
boolean containsValue(Object Value) 如果此地图将一个或多个键映射到指定的值,则返回 true 。
boolean isEmpty() 如果此地图不包含键值映射,则返回 true 。
D: 获取功能
Set<Map.Entry<K,V>> entrySet() 返回此地图中包含的映射的Set视图。 V get(Object key) 返回到指定键所映射的值,或 null如果此映射包含该键的映射。 Set<K> keySet() 返回此地图中包含的键的Set视图。 Collection<V> values() 返回此地图中包含的值的Collection视图。
E: 长度功能
int size() 返回此地图中键值映射的数量。
1 package com.wyh.mapSummary; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 import java.util.Set; 6 7 /** 8 * @author WYH 9 * @version 2019年11月20日 上午9:44:41 10 * 11 * * 1、添加功能 12 * V put(K key, V value) 将指定的值与该映射中的指定键相关联(可选操作)。 13 * 2、删除功能 14 * void clear() 从该地图中删除所有的映射(可选操作)。 15 * V remove(Object key) 如果存在(从可选的操作),从该地图中删除一个键的映射。 16 * 3、判断功能 17 * boolean containsKey(Object key) 如果此映射包含指定键的映射,则返回 true 。 18 * boolean containsValue(Object Value) 如果此地图将一个或多个键映射到指定的值,则返回 true 。 19 * boolean isEmpty() 如果此地图不包含键值映射,则返回 true 。 20 * 4、获取功能 21 * Set<Map.Entry<K,V>> entrySet() 返回此地图中包含的映射的Set视图。 22 * V get(Object key) 返回到指定键所映射的值,或 null如果此映射包含该键的映射。 23 * Set<K> keySet() 返回此地图中包含的键的Set视图。 24 * Collection<V> values() 返回此地图中包含的值的Collection视图。 25 * 5、长度功能 26 * int size() 返回此地图中键值映射的数量。 27 */ 28 public class Map01 { 29 public static void main(String[] args) { 30 // 31 Map<String,String> map = new HashMap<String,String>(); 32 33 34 //V put(K key, V value) 将指定的值与该映射中的指定键相关联(可选操作)。 35 map.put("王友虎", "超人"); 36 map.put("赵以浩", "绿巨人"); 37 map.put("李宏灿", "奇异博士"); 38 map.put("齐博源", "雷神"); 39 40 //void clear() 从该地图中删除所有的映射(可选操作)。 41 //map.clear(); 42 43 //V remove(Object key) 如果存在(从可选的操作),从该地图中删除一个键的映射。 44 //map.remove("王友虎"); 45 46 //boolean containsKey(Object key) 如果此映射包含指定键的映射,则返回 true 。 47 //System.out.println("containsKey:"+map.containsKey("王友虎")); 48 49 //boolean isEmpty() 如果此地图不包含键值映射,则返回 true 。 50 //System.out.println("isEmpty:"+map.isEmpty()); 51 52 53 //int size() 返回此地图中键值映射的数量。 54 //System.out.println("size:"+map.size()); 55 56 57 //Set<K> keySet() 返回此地图中包含的键的Set视图。 58 Set<String> set = map.keySet(); 59 for(String s : set) { 60 //V get(Object key) 返回到指定键所映射的值,或 null如果此映射包含该键的映射。 61 System.out.println(s+"---"+map.get(s)); 62 } 63 64 65 System.out.println(map); 66 67 68 } 69 70 }
(4)Map集合的遍历
A: 键找值
a: 获取所有键的集合
b: 遍历键的集合,得到每一个键
c: 根据键到集合中去找值
1 package com.wyh.map; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 import java.util.Set; 6 7 /** 8 * @author WYH 9 * @version 2019年11月17日 下午7:51:02 10 11 * 12 * 13 * 根据键找值 14 */ 15 public class MapDemo3 16 { 17 public static void main(String[] args) { 18 //创建map集合 19 Map<String,String> map = new HashMap<String,String>(); 20 21 //添加元素 22 map.put("小月", "小美"); 23 map.put("李晨", "范冰冰"); 24 map.put("邓超","孙俪"); 25 map.put("黄晓明", "杨颖"); 26 27 //获取到所有的key 28 Set<String> set = map.keySet(); 29 30 //遍历 31 for(String key : set) { 32 String s = map.get(key); 33 System.out.println(key+"---"+s); 34 } 35 36 } 37 38 }
B: 键值对对象找键和值
a: 获取所有的键值对对象的集合
b: 遍历键值对对象的集合,获取每一个键值对对象
c: 根据键值对对象去获取键和值
1 package com.wyh.map; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 import java.util.Set; 6 7 /** 8 * @author WYH 9 * @version 2019年11月17日 下午8:13:52 10 * 11 * 根据键值对对象找键和值 12 */ 13 public class MapDemo4 { 14 public static void main(String[] args) { 15 Map<String,String> map = new HashMap<String,String>(); 16 17 //添加元素 18 map.put("小月", "小美"); 19 map.put("李晨", "范冰冰"); 20 map.put("邓超","孙俪"); 21 map.put("黄晓明", "杨颖"); 22 23 Set<Map.Entry<String,String>> set = map.entrySet(); 24 25 for(Map.Entry<String,String> me : set) { 26 System.out.println(me.getKey()+"---"+me.getValue()); 27 } 28 } 29 30 }
(5)HashMap集合的练习
A: HashMap<String,String>
1 package com.wyh.mapSummary; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 import java.util.Set; 6 7 /** 8 * @author WYH 9 * @version 2019年11月20日 上午10:38:29 10 */ 11 public class Map02 { 12 public static void main(String[] args) { 13 Map<String, String> map = new HashMap<String, String>(); 14 15 // 添加元素 16 map.put("小月", "小美"); 17 map.put("李晨", "范冰冰"); 18 map.put("邓超", "孙俪"); 19 map.put("黄晓明", "杨颖"); 20 21 Set<Map.Entry<String, String>> set = map.entrySet(); 22 23 for (Map.Entry<String, String> me : set) { 24 System.out.println(me.getKey() + "---" + me.getValue()); 25 } 26 } 27 28 }
B: HashMap<Integer,String>
1 package com.wyh.mapSummary; 2 3 import java.util.HashMap; 4 import java.util.Set; 5 6 /** 7 * @author WYH 8 * @version 2019年11月20日 上午11:02:16 9 */ 10 public class Map03 { 11 public static void main(String[] args) { 12 HashMap<Integer,String> hm = new HashMap<Integer,String>(); 13 14 hm.put(1, "王友虎"); 15 hm.put(2, "赵以浩"); 16 hm.put(3, "齐博源"); 17 hm.put(4, "李先锋"); 18 hm.put(5, "李宏灿"); 19 hm.put(6, "张国兴"); 20 21 Set<Integer> set = hm.keySet(); 22 23 for(Integer i : set) { 24 System.out.println(i+"---"+hm.get(i)); 25 } 26 27 } 28 29 }
C: HashMap<String,Student>
1 package com.wyh.mapSummary; 2 3 import java.util.HashMap; 4 import java.util.Set; 5 6 import com.wyh.map.Student; 7 8 /** 9 * @author WYH 10 * @version 2019年11月20日 上午11:13:55 11 */ 12 public class Map04 { 13 public static void main(String[] args) { 14 HashMap<String, Student> hm = new HashMap<String, Student>(); 15 16 // 创建对象 17 Student s1 = new Student("王友虎", 22); 18 Student s2 = new Student("赵以浩", 24); 19 Student s3 = new Student("齐博源", 21); 20 Student s4 = new Student("李先锋", 22); 21 Student s5 = new Student("李宏灿", 22); 22 23 // 添加到集合中去 24 hm.put("1", s1); 25 hm.put("2", s2); 26 hm.put("3", s3); 27 hm.put("4", s4); 28 hm.put("5", s5); 29 30 Set<String> set = hm.keySet(); 31 for(String s : set) { 32 System.out.println(s+"--"+hm.get(s).getName()+"--"+hm.get(s).getAge()); 33 } 34 35 36 } 37 38 }
D: HashMap<Student,String> 容易忽略一点,为了保证键值相同,是一个学生类,而且是由HashMap存储, 就要重写,hashCode()和equals()方法
学生类:
1 package com.wyh.mapSummary; 2 3 /** 4 * @author WYH 5 * @version 2019年11月19日 下午7:01:40 6 */ 7 public class Student { 8 private String name; 9 private int age; 10 public Student(String name, int age) { 11 super(); 12 this.name = name; 13 this.age = age; 14 } 15 public Student() { 16 super(); 17 // TODO Auto-generated constructor stub 18 } 19 /** 20 * @return the name 21 */ 22 public String getName() { 23 return name; 24 } 25 /** 26 * @param name the name to set 27 */ 28 public void setName(String name) { 29 this.name = name; 30 } 31 /** 32 * @return the age 33 */ 34 public int getAge() { 35 return age; 36 } 37 /** 38 * @param age the age to set 39 */ 40 public void setAge(int age) { 41 this.age = age; 42 } 43 @Override 44 public int hashCode() { 45 final int prime = 31; 46 int result = 1; 47 result = prime * result + age; 48 result = prime * result + ((name == null) ? 0 : name.hashCode()); 49 return result; 50 } 51 @Override 52 public boolean equals(Object obj) { 53 if (this == obj) 54 return true; 55 if (obj == null) 56 return false; 57 if (getClass() != obj.getClass()) 58 return false; 59 Student other = (Student) obj; 60 if (age != other.age) 61 return false; 62 if (name == null) { 63 if (other.name != null) 64 return false; 65 } else if (!name.equals(other.name)) 66 return false; 67 return true; 68 } 69 70 71 72 73 }
测试类:
1 package com.wyh.map; 2 3 import java.util.HashMap; 4 import java.util.Set; 5 6 /** 7 * @author WYH 8 * @version 2019年11月19日 下午7:20:14 9 * 10 * <Student,String> 11 * 12 * 容易忽略一点,为了保证键值相同,是一个学生类,而且是由HashMap存储, 就要重写,hashCode()和equals()方法 13 */ 14 public class HashMapDemo2 { 15 public static void main(String[] args) { 16 //创建集合对象 17 HashMap<Student,String> hm = new HashMap<Student,String>(); 18 19 //创建对象 20 Student s1 = new Student("王友虎",22); 21 Student s2 = new Student("赵以浩",24); 22 Student s3 = new Student("齐博源",21); 23 Student s4 = new Student("李先锋",22); 24 Student s5 = new Student("李宏灿",22); 25 Student s6 = new Student("王友虎",22); 26 27 //添加到集合 28 hm.put(s1, "001"); 29 hm.put(s2, "002"); 30 hm.put(s3, "003"); 31 hm.put(s4, "004"); 32 hm.put(s5, "005"); 33 hm.put(s6, "006"); 34 35 Set<Student> set = hm.keySet(); 36 37 for(Student key : set) { 38 String s = hm.get(key); 39 System.out.println(key.getName()+"---"+key.getAge()+"---"+s); 40 } 41 42 } 43 44 }
(6)TreeMap的练习
A: TreeMap<String,String>
1 package com.wyh.mapSummary; 2 3 import java.util.Set; 4 import java.util.TreeMap; 5 6 /** 7 * @author WYH 8 * @version 2019年11月20日 下午2:06:17 9 */ 10 public class TreeMapDemo { 11 public static void main(String[] args) { 12 TreeMap<String,String> tm = new TreeMap<String,String>(); 13 14 tm.put("123", "张三"); 15 tm.put("143", "李四"); 16 tm.put("123", "张三"); 17 tm.put("1123", "489"); 18 tm.put("1423", "789"); 19 tm.put("133", "999"); 20 21 22 Set<String> s = tm.keySet(); 23 24 for(String s1 : s) { 25 System.out.println(tm.get(s1)); 26 27 } 28 29 30 31 32 33 34 35 } 36 37 }
B: TreeMap<Student,String> 无参构造方法默认调用的是自然排序,但是这里要自己重写方法
1 package com.wyh.map; 2 3 import java.util.Comparator; 4 5 import java.util.Set; 6 import java.util.TreeMap; 7 8 /** 9 * @author WYH 10 * @version 2019年11月19日 下午7:54:29 11 * 无参构造方法默认调用的是自然排序,但是这里要自己重写方法 12 * 13 * 14 */ 15 public class TreeMapDemo2 { 16 public static void main(String[] args) { 17 TreeMap<Student,String> tm = new TreeMap<Student,String>(new Comparator<Student>() { 18 19 @Override 20 public int compare(Student s1, Student s2) { 21 int num = s1.getName().length() - s2.getName().length(); 22 int num1 = num == 0 ? s1.getName().compareTo(s2.getName()) : num; 23 int num2 = num1 == 0 ? s1.getAge() - s2.getAge() : num1; 24 return num2; 25 } 26 27 28 }); 29 30 //创建对象 31 Student s1 = new Student("王友虎",22); 32 Student s2 = new Student("赵以浩",24); 33 Student s3 = new Student("齐博源",21); 34 Student s4 = new Student("李先锋",22); 35 Student s5 = new Student("李宏灿",22); 36 Student s6 = new Student("王友虎",22); 37 38 tm.put(s1, "六期"); 39 tm.put(s2, "五期"); 40 tm.put(s3, "六期"); 41 tm.put(s4, "七期"); 42 tm.put(s5, "六期"); 43 tm.put(s6, "六期"); 44 45 46 47 Set<Student> set = tm.keySet(); 48 49 for(Student s : set) { 50 System.out.println(s.getName()+"---"+s.getAge()+"---"+tm.get(s)); 51 } 52 53 54 55 56 } 57 58 }