Map<k , v>集合
Map集合与Collection集合不同,collection是单列集合,map是双列集合,一个元素包含两个值(key,value)。其特点有:
1.双列集合,一个元素包含两个值(key,value)
2.key,value的数据类型可以相同,也可以不同。
3.Map中的元素,key是不允许重复的,value是允许重复的。
4.key—value是一一对应的。
Map的常用子类有:HashMap,LinkedHashMap。
HashMap:1.底层是哈希表结构,查询速度快,2.是一个无序的集合,存储元素和取出元素的顺序可能不一致。
LinkedHashMap:1.是HashMap的子类,底层是哈希表+链表2.是一个有序的集合,存储元素和取出元素的顺序是一致的。
map的常用方法有:
1.V put(K,V),向HashMap中添加元素
2.V get(K)得到key对应的value值,没有返回null
3.V remove(K)移除key以及所对应的value,返回value值,没有返回null
4.boolean containsKey(K)判断是否有key,有返回true,没有返回false
5.Set< K > keySet()取出Key组成一个Collection.Set集合
6.Set<Map.Entry<K, V>> entrySet()取出Map.Entry<K, V>组成一个Collection.Set集合
public static void main(String[] args) {
HashMap<String, Integer> list = new HashMap<>();
//1.V put(K,V),向HashMap中添加元素
// 如果无key,就添加key,value
// 有key则修改对应的value,返回旧value值
list.put("阿明",15);
Integer age = list.put("阿明", 16);
System.out.println(age);
list.put("阿花",116);
list.put("阿康",17);
list.put("阿洋",18);
list.put("阿哲",19);
System.out.println(list);
System.out.println("===================");
//2.V get(K)得到key对应的value值,没有返回null
Integer age2 = list.get("阿花");
System.out.println(age2);
Integer age3 = list.get("阿拉");
System.out.println(age3);
System.out.println(list);
System.out.println("===================");
//3.V remove(K)移除key以及所对应的value,返回value值,没有返回null
Integer age4 = list.remove("阿花");
System.out.println(age4);
Integer age5 = list.remove("阿拉");
System.out.println(age5);
System.out.println(list);
System.out.println("===================");
//4.boolean containsKey(K)判断是否有key,有返回true,没有返回false
boolean bool = list.containsKey("阿明");
System.out.println(bool);
boolean bool2 = list.containsKey("阿拉");
System.out.println(bool2);
System.out.println(list);
}
public static void main(String[] args) {
HashMap<String, String> list = new HashMap<>();
list.put("aa","AA");
list.put("bb","BB");
list.put("cc","CC");
list.put("dd","DD");
//取出Key组成一个Set集合
Set<String> strings = list.keySet();
for (String s : strings) {
System.out.println(s);
System.out.println(list.get(s));
}
}
Map.Entry是map接口的一个内部接口,当Map对象一创建,就会创建一个Entry对象记录键值对对象(键与值的映射关系),如果说Map是记录所有的键值对映射关系,那么Entry就记录了其中的每一个映射关系,许许多多个Entry组成了Map。
public static void main(String[] args) {
HashMap<String, String> list = new HashMap<>();
list.put("aa","AA");
list.put("bb","BB");
list.put("cc","CC");
list.put("dd","DD");
Set<Map.Entry<String, String>> entries = list.entrySet();
Iterator<Map.Entry<String, String>> iterator = entries.iterator();
while (iterator.hasNext()){
Map.Entry<String, String> next = iterator.next();
System.out.println(next.getKey());
System.out.println(next.getValue());
}
}
HashMap集合存储自定义类型键值,最关键一点,key是不允许相同的,即key的数据类型必须重写hashCode()和equals()方法。
//Person类
public class Person {
private String name;
private int age;
@Override
public 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);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public Person() {
}
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;
}
}
//main
public static void main(String[] args) {
//HashMap存储自定义类型键值
//1.key为String,key不允许重复,String类重写了hashCode()和equals()方法
// Value为Person,value允许重复,无需重写
HashMap<String, Person> map = new HashMap<>();
map.put("第一名", new Person("阿明", 18));
map.put("第二名", new Person("阿花", 19));
map.put("第三名", new Person("阿明", 14));
map.put("第一名", new Person("阿拉", 16));
//keySet遍历
Set<String> set = map.keySet();
for (String s : set) {
System.out.println(s + "-->" + map.get(s));
}
//2.key为Person,key不允许重复,Person类需重写hashCode()和equals()方法
//Value为Integer,value允许重复,无需重写
HashMap<Person, Integer> hashMap = new HashMap<>();
hashMap.put(new Person("阿明", 18),98);
hashMap.put(new Person("阿花", 19),98);
hashMap.put(new Person("阿明", 18),62);
hashMap.put(new Person("阿拉", 16),59);
//entrySet
Set<Map.Entry<Person, Integer>> entrySet = hashMap.entrySet();
Iterator<Map.Entry<Person, Integer>> iterator = entrySet.iterator();
while (iterator.hasNext()){
Map.Entry<Person, Integer> next = iterator.next();
System.out.println(next.getKey() + "==" + next.getValue());
}
}
LinkedHashMap:1.是HashMap的子类,底层是哈希表+链表2.是一个有序的集合,存储元素和取出元素的顺序是一致的:
public static void main(String[] args) {
HashMap<String, Integer> hashMap = new HashMap<>();
hashMap.put("a",7);
hashMap.put("c",6);
hashMap.put("b",5);
hashMap.put("a",6);
System.out.println(hashMap);//无序的集合,{a=6, b=5, c=6}
LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("a",7);
map.put("c",6);
map.put("b",5);
map.put("a",6);
System.out.println(map);//有序的集合,{a=6, c=6, b=5},是HashMap的子类
}
Map中逐渐被淘汰的的集合Hashtable:是单线程的,查询速度慢,K,V不允许为空:
public static void main(String[] args) {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put(null,"a");
hashMap.put("a",null);
System.out.println(hashMap);//{null=a, a=null}
Hashtable<String, String> table = new Hashtable<>();
table.put("a",null);
System.out.println(table);//NullPointerException
}