JavaSE16-集合·其三
1.Map集合
1.1 Map集合概述和特点
1 interface Map<K,V> K:键的类型;V:值的类型
Map集合的特点
键值对映射关系
一个键对应一个值
键不能重复,值可以重复 元素存取无序
1.2 Map集合的基本功能
| 方法名 | 说明 |
| V put(K key,V value) | 添加元素 |
| V remove(Object key) | 根据键删除键值对元素 |
| void clear() | 移除所有的键值对元素 |
| boolean containsKey(Object key) | 判断集合是否包含指定的键 |
| boolean containsValue(Object value) | 判断集合是否包含指定的值 |
| boolean isEmpty() | 判断集合是否为空 |
| int size() | 集合的长度,也就是集合中键值对的个数 |
1.3 Map集合的获取功能
| 方法名 | 说明 |
| V get(Object key) | 根据键获取值 |
| Set keySet() | 获取所有键的集合 |
| Collection values() | 获取所有值的集合 |
| Set<map.entry<k,v>> entrySet() | 获取所有键值对对象的集合 |
1.4 Map集合的遍历(方式1)
遍历思路
我们刚才存储的元素都是成对出现的,所以我们把Map看成是一个夫妻对的集合
把所有的丈夫给集中起来遍历丈夫的集合,获取到每一个丈夫
根据丈夫去找对应的妻子
步骤分析
获取所有键的集合。用keySet()方法实现。
1 import java.util.HashMap; 2 import java.util.HashSet; 3 import java.util.Map; 4 import java.util.Set; 5 6 public class kb { 7 public static void main(String[] args) { 8 Map<String,String> sl = new HashMap<>(); 9 sl.put("1","超人"); 10 sl.put("2","蝙蝠侠"); 11 sl.put("3","闪电侠"); 12 13 Set<String> key = new HashSet<>(); 14 key = sl.keySet(); 15 16 for (String id : key){ 17 System.out.println(id+sl.get(id)); 18 } 19 } 20 }
1.5 Map集合的遍历(方式2)
遍历思路
我们刚才存储的元素都是成对出现的,所以我们把Map看成是一个夫妻对的集合
获取所有结婚证的集合
遍历结婚证的集合,得到每一个结婚证
根据结婚证获取丈夫和妻子
步骤分析
获取所有键值对对象的集合
Set<map.entry<k,v>>entrySet():获取所有键值对对象的集合
遍历键值对对象的集合,得到每一个键值对对象
用增强for实现,得到每一个Map.Entry
根据键值对对象获取键和值
用getKey()得到键
用getValue()得到值
1 import java.util.HashMap; 2 import java.util.HashSet; 3 import java.util.Map; 4 import java.util.Set; 5 6 public class kb { 7 public static void main(String[] args) { 8 Map<String, String> sl = new HashMap<>(); 9 sl.put("1", "超人"); 10 sl.put("2", "蝙蝠侠"); 11 sl.put("3", "闪电侠"); 12 13 Set<Map.Entry<String, String>> entrySet = sl.entrySet(); 14 15 for (Map.Entry<String, String> me : entrySet) { 16 System.out.println(me.getKey() + "\t" + me.getValue()); 17 } 18 } 19 }
2.Collections集合工具类
2.1 Collections概述和使用
Collections类的作用
是针对集合操作的工具类
常用方法
| 方法名 | 说明 |
| public static void sort(List list) | 将指定的列表按升序排序 |
| public static void reverse(List list) | 反转指定列表中元素的顺序 |
| public static void shuffle(List list) | 使用默认的随机源随机排列指定的列表 |
2.2 ArrayList集合存储学生并排序
案例需求
ArrayList存储学生对象,使用Collections对ArrayList进行排序
要求:按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序
学生类
1 public class Student { 2 private String name; 3 private int age; 4 5 public Student() { 6 } 7 8 public Student(String name, int age) { 9 this.name = name; 10 this.age = age; 11 } 12 13 public String getName() { 14 return name; 15 } 16 17 public void setName(String name) { 18 this.name = name; 19 } 20 21 public int getAge() { 22 return age; 23 } 24 25 public void setAge(int age) { 26 this.age = age; 27 } 28 29 @Override 30 public boolean equals(Object o) { 31 if (this == o) return true; 32 if (o == null || getClass() != o.getClass()) return false; 33 34 Student student = (Student) o; 35 36 if (age != student.age) return false; 37 return name != null ? name.equals(student.name) : student.name == null; 38 } 39 40 @Override 41 public int hashCode() { 42 int result = name != null ? name.hashCode() : 0; 43 result = 31 * result + age; 44 return result; 45 } 46 47 @Override 48 public String toString() { 49 return "Student{" + 50 "name='" + name + '\'' + 51 ", age=" + age + 52 '}'; 53 } 54 }
排序测试类
1 import java.util.*; 2 3 public class newComparator { 4 public static void main(String[] args) { 5 Student s1 = new Student("Tony",21); 6 Student s2 = new Student("xiaohong",21); 7 Student s3 = new Student("wangdachui",800); 8 9 ArrayList<Student> stu = new ArrayList<>(); 10 11 stu.add(s1); 12 stu.add(s2); 13 stu.add(s3); 14 15 Collections.sort(stu, new Comparator<Student>() { 16 @Override 17 public int compare(Student o1, Student o2) { 18 int num = o1.getAge() - o2.getAge(); 19 int num2 = num == 0 ?o1.getName().compareTo(o2.getName()):num; 20 return num2; 21 } 22 }); 23 24 for (Student s : stu){ 25 System.out.println(s.getName()+"\t"+s.getAge()); 26 } 27 } 28 }

浙公网安备 33010602011771号