1.Map汇合
1.1Map汇合概述和特点
Map汇合概述
interface Map<K,V> K:键的类型;V:值的类型
Map汇合的特点
- 双列汇合,一个键对应一个值
- 键不能够反复,值能够反复
Map汇合的根本应用
public class MapDemo01 { public static void main(String[] args) { //创立汇合对象 Map<String,String> map = new HashMap<String,String>(); //V put(K key, V value) 将指定的值与该映射中的指定键相关联 map.put("itheima001","林青霞"); map.put("itheima002","张曼玉"); map.put("itheima003","王祖贤"); map.put("itheima003","柳岩"); //输入汇合对象 System.out.println(map); }}
1.2Map汇合的基本功能
办法介绍
办法名 阐明 V put(K key,V value) 增加元素 V remove(Object key) 依据键删除键值对元素 void clear() 移除所有的键值对元素 boolean containsKey(Object key) 判断汇合是否蕴含指定的键 boolean containsValue(Object value) 判断汇合是否蕴含指定的值 boolean isEmpty() 判断汇合是否为空 int size() 汇合的长度,也就是汇合中键值对的个数 示例代码
public class MapDemo02 { public static void main(String[] args) { //创立汇合对象 Map<String,String> map = new HashMap<String,String>(); //V put(K key,V value):增加元素 map.put("张无忌","赵敏"); map.put("郭靖","黄蓉"); map.put("杨过","小龙女"); //V remove(Object key):依据键删除键值对元素// System.out.println(map.remove("郭靖"));// System.out.println(map.remove("郭襄")); //void clear():移除所有的键值对元素// map.clear(); //boolean containsKey(Object key):判断汇合是否蕴含指定的键// System.out.println(map.containsKey("郭靖"));// System.out.println(map.containsKey("郭襄")); //boolean isEmpty():判断汇合是否为空// System.out.println(map.isEmpty()); //int size():汇合的长度,也就是汇合中键值对的个数 System.out.println(map.size()); //输入汇合对象 System.out.println(map); }}
1.3Map汇合的获取性能
办法介绍
办法名 阐明 V get(Object key) 依据键获取值 Set<K> keySet() 获取所有键的汇合 Collection<V> values() 获取所有值的汇合 Set<Map.Entry<K,V>> entrySet() 获取所有键值对对象的汇合 示例代码
public class MapDemo03 { public static void main(String[] args) { //创立汇合对象 Map<String, String> map = new HashMap<String, String>(); //增加元素 map.put("张无忌", "赵敏"); map.put("郭靖", "黄蓉"); map.put("杨过", "小龙女"); //V get(Object key):依据键获取值// System.out.println(map.get("张无忌"));// System.out.println(map.get("张三丰")); //Set<K> keySet():获取所有键的汇合// Set<String> keySet = map.keySet();// for(String key : keySet) {// System.out.println(key);// } //Collection<V> values():获取所有值的汇合 Collection<String> values = map.values(); for(String value : values) { System.out.println(value); } }}
1.4Map汇合的遍历(形式1)
遍历思路
咱们方才存储的元素都是成对呈现的,所以咱们把Map看成是一个夫妻对的汇合
- 把所有的丈夫给集中起来
- 遍历丈夫的汇合,获取到每一个丈夫
- 依据丈夫去找对应的妻子
步骤剖析
- 获取所有键的汇合。用keySet()办法实现
- 遍历键的汇合,获取到每一个键。用加强for实现
- 依据键去找值。用get(Object key)办法实现
代码实现
public class MapDemo01 { public static void main(String[] args) { //创立汇合对象 Map<String, String> map = new HashMap<String, String>(); //增加元素 map.put("张无忌", "赵敏"); map.put("郭靖", "黄蓉"); map.put("杨过", "小龙女"); //获取所有键的汇合。用keySet()办法实现 Set<String> keySet = map.keySet(); //遍历键的汇合,获取到每一个键。用加强for实现 for (String key : keySet) { //依据键去找值。用get(Object key)办法实现 String value = map.get(key); System.out.println(key + "," + value); } }}
1.5Map汇合的遍历(形式2)
遍历思路
咱们方才存储的元素都是成对呈现的,所以咱们把Map看成是一个夫妻对的汇合
- 获取所有结婚证的汇合
- 遍历结婚证的汇合,失去每一个结婚证
- 依据结婚证获取丈夫和妻子
步骤剖析
获取所有键值对对象的汇合
- Set<Map.Entry<K,V>> entrySet():获取所有键值对对象的汇合
遍历键值对对象的汇合,失去每一个键值对对象
- 用加强for实现,失去每一个Map.Entry
依据键值对对象获取键和值
- 用getKey()失去键
- 用getValue()失去值
代码实现
public class MapDemo02 { public static void main(String[] args) { //创立汇合对象 Map<String, String> map = new HashMap<String, String>(); //增加元素 map.put("张无忌", "赵敏"); map.put("郭靖", "黄蓉"); map.put("杨过", "小龙女"); //获取所有键值对对象的汇合 Set<Map.Entry<String, String>> entrySet = map.entrySet(); //遍历键值对对象的汇合,失去每一个键值对对象 for (Map.Entry<String, String> me : entrySet) { //依据键值对对象获取键和值 String key = me.getKey(); String value = me.getValue(); System.out.println(key + "," + value); } }}
2.HashMap汇合
2.1HashMap汇合概述和特点
- HashMap底层是哈希表构造的
- 依赖hashCode办法和equals办法保障键的惟一
- 如果键要存储的是自定义对象,须要重写hashCode和equals办法
2.2HashMap汇合利用案例
案例需要
- 创立一个HashMap汇合,键是学生对象(Student),值是居住地 (String)。存储多个元素,并遍历。
- 要求保障键的唯一性:如果学生对象的成员变量值雷同,咱们就认为是同一个对象
代码实现
学生类
public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; if (age != student.age) return false; return name != null ? name.equals(student.name) : student.name == null; } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + age; return result; }}
测试类
public class HashMapDemo { public static void main(String[] args) { //创立HashMap汇合对象 HashMap<Student, String> hm = new HashMap<Student, String>(); //创立学生对象 Student s1 = new Student("林青霞", 30); Student s2 = new Student("张曼玉", 35); Student s3 = new Student("王祖贤", 33); Student s4 = new Student("王祖贤", 33); //把学生增加到汇合 hm.put(s1, "西安"); hm.put(s2, "武汉"); hm.put(s3, "郑州"); hm.put(s4, "北京"); //遍历汇合 Set<Student> keySet = hm.keySet(); for (Student key : keySet) { String value = hm.get(key); System.out.println(key.getName() + "," + key.getAge() + "," + value); } }}
3.TreeMap汇合
3.1TreeMap汇合概述和特点
- TreeMap底层是红黑树结构
- 依赖天然排序或者比拟器排序,对键进行排序
- 如果键存储的是自定义对象,须要实现Comparable接口或者在创立TreeMap对象时候给出比拟器排序规定
3.2TreeMap汇合利用案例
案例需要
- 创立一个TreeMap汇合,键是学生对象(Student),值是籍贯(String),学生属性姓名和年龄,依照年龄进行排序并遍历
- 要求依照学生的年龄进行排序,如果年龄雷同则依照姓名进行排序
代码实现
学生类
public class Student implements Comparable<Student>{ private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } @Override public int compareTo(Student o) { //依照年龄进行排序 int result = o.getAge() - this.getAge(); //主要条件,依照姓名排序。 result = result == 0 ? o.getName().compareTo(this.getName()) : result; return result; }}
测试类
public class Test1 { public static void main(String[] args) { // 创立TreeMap汇合对象 TreeMap<Student,String> tm = new TreeMap<>(); // 创立学生对象 Student s1 = new Student("xiaohei",23); Student s2 = new Student("dapang",22); Student s3 = new Student("xiaomei",22); // 将学生对象增加到TreeMap汇合中 tm.put(s1,"江苏"); tm.put(s2,"北京"); tm.put(s3,"天津"); // 遍历TreeMap汇合,打印每个学生的信息 tm.forEach( (Student key, String value)->{ System.out.println(key + "---" + value); } ); }}