Map
1、Map集合概述
java.util.Map<K, V>集合,里面保存的数据是成对存在的,称之为双列集合,我们称之为键值对。而Collection集合中的元素是单个存在的,称之为单列集合。
2、Map集合的特点
1、可以存储两个数据
2、key元素不能重复,value可以重复
3、一个key元素值对应一个value元素
4、存取元素不保证顺序
5、没有索引
6、Map集合不能直接遍历
3、Map集合常用实现类
1、HashMap:
HashSet底层实现就是HashMap完成的,HashSet保存的元素其实就是HashMap集合中保存的键,底层结构是哈希表结构,具有键唯一,无序的特点。
2、LinkedHashMap:
底层结构是链表和哈希表结构,去重,有序。
3、TreeMap
底层是红黑树,去重,通过键排序。
4、Map集合中常用的方法
4.1、Map中常用的方法
方法 | 说明 |
---|---|
public V put (K key, V value) | 把指定的键和指定的值添加到Map集合中 |
public V remove (Object key) | 把指定的键所对应的键值对元素在Map集合中删除,返回被删除的元素的值 |
public V get (Object k) | 根据指定的键在Map集合中获取指定的值 |
public Set< K > keySet () | 获取Map中所有的键存储在Set集合中 |
public Set<Map.Entry<K, V>> entrySet () | 获取Map中所有的键值对对象集合 |
public boolean containKey(Object key) | 判断该集合中是否有此键 |
4.2、简单代码示例
public static void main(String[] args) {Map<String, String> map = new HashMap<>();//添加元素map.put("哇", "糖锅好厉害!");map.put("啊", "糖锅人真好!");map.put("哈", "糖锅人真好!");map.put("喔", "糖锅真男人!");//key元素相同时,会用新的value值覆盖之前的value值map.put("哇", "糖锅真牛啊!");System.out.println(map);//删除map集合中的元素map.remove("喔");System.out.println(map);//获取集合中指定元素String demo = map.get("哇");System.out.println(demo);}
5、Map集合的遍历
5.1、键找值
获取Map集合中所有的key元素,遍历所有的key元素,通过key元素找到对应的value值。
//获取map集合中所有的keySet<String> set = map.keySet();for (String s : set) {System.out.println(s);}
5.2、键值对
获取Map集合中所有的Map.Entry,遍历所有的Map.Entry,通过Entry中的API方法获取对应的key和value.
Set<Map.Entry<String, String>> set1 = map.entrySet();
for (Map.Entry<String, String> entry : set1) {System.out.println(entry.getKey() + " = " + entry.getValue());}
6、Map集合存储自定义类型
代码示例:每个人(姓名、年龄)都有自己的爱好,人和爱好有对应关系,将人对象和兴趣爱好存储在map集合中。人作为键,兴趣爱好作为值。
要求:人的姓名和年龄相同视为同一人,不能重复存储
Person类
public class Person {String name;int age;public Person(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;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +'}';}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Person person = (Person) o;return age == person.age && Objects.equals(name, person.name);}@Overridepublic int hashCode() {return Objects.hash(name, age);}
}
测试类
public class MapDemo1 {public static void main(String[] args) {Map<Person, String> map = new HashMap<>();map.put(new Person("爱坤", 23), "哎哟~你干嘛~");map.put(new Person("坤坤", 23), "爱大篮球");map.put(new Person("鸡哥", 23), "爱唱跳rap");Set<Person> people = map.keySet();for (Person person : people) {System.out.println(person + "--->" + map.get(person));}}
}
注意:在Map集合中当key存储的是自定义对象时,要保证数据存储的唯一性,需要自定义对象中重写hashcode、equals方法。
LinkedHashMap与HashMap语法规则相似,可对照HashMap使用。
7、TreeMap
7.1、介绍
有排序功能,键去重
1、可以自然排序(键所在的类要实现Comparable)
public class MapDemo2 {public static void main(String[] args) {TreeMap<Integer, String> treeMap = new TreeMap<>();treeMap.put(3, "hhh");treeMap.put(1, "aaa");treeMap.put(13, "jjj");treeMap.put(8, "uuu");treeMap.put(9, "www");System.out.println(treeMap);}
}
运行结果
{1=aaa, 3=hhh, 8=uuu, 9=www, 13=jjj}
如果想要倒序排序可以这样写
public class MapDemo2 {public static void main(String[] args) {TreeMap<Integer, String> treeMap = new TreeMap<>(new Comparator<Integer>() {@Overridepublic int compare(Integer a, Integer b) {return b - a;}});treeMap.put(3, "hhh");treeMap.put(1, "aaa");treeMap.put(13, "jjj");treeMap.put(8, "uuu");treeMap.put(9, "www");System.out.println(treeMap);}
}
运行结果
{13=jjj, 9=www, 8=uuu, 3=hhh, 1=aaa}
2、若自定义类没有自然排序功能,或自然排序功能不满足我们的需求时。我们可以自定义比较器排序(Comparator)
使用6中的Person类,将其存储于一个TreeMap集合中去,集合按照Person类中年龄大小升序排序,若年龄相同则进行降序排序。
public class MapDemo3 {public static void main(String[] args) {TreeMap<Person, String> treeMap = new TreeMap<>(new Comparator<Person>() {@Overridepublic int compare(Person p1, Person p2) {int res = p1.age - p2.age;if(res == 0){res = p2.name.compareTo(p1.name);}return res;}});treeMap.put(new Person("aaa", 24), "测试数据1");treeMap.put(new Person("bbb", 24), "测试数据2");treeMap.put(new Person("ccc", 24), "测试数据3");treeMap.put(new Person("ddd", 23), "测试数据4");treeMap.put(new Person("eee", 89), "测试数据5");Set<Person> people = treeMap.keySet();for (Person person : people) {System.out.println(person);}}
}
运行结果
Person{name='ddd', age=23}
Person{name='ccc', age=24}
Person{name='bbb', age=24}
Person{name='aaa', age=24}
Person{name='eee', age=89}
两种排序方式对应了TreeMap两个构造方法:
public TreeMap()//使用自然排序
public TreeMap(Comparator<? super K> comparator) //比较器