Map接口常用方法
1.Map的特点
- Map和Collection没有继承关系
- 以键值对的方式存储
- 键和值都是引用类型
2.Map接口常用方法
常用方法测试,以及map的遍历
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
class Main{
public static void main(String[] args) {
//创建Map集合对象
Map<Integer,String> map=new HashMap<>();
//放入元素
map.put(1,"abc");
map.put(2,"def");
map.put(3,"ghi");
map.put(4,"ssd");
map.put(5,"vrf");
map.put(6,"erf");
//通过key获取value
System.out.println(map.get(1));
//获取键值对的数量
System.out.println("键值对的数量:"+map.size());
//通过key删除value
map.remove(1);
//判断是否包含某个key
System.out.println(map.containsKey(2));//true
//判断是否包含某个value
System.out.println(map.containsValue("abc"));//false
//清空map集合
//map.clear();
//判断是否为空
System.out.println(map.isEmpty()?"集合为空":"集合非空");
//map集合的遍历
//第一种:先获取所有的key,通过key遍历value
Set<Integer> keys=map.keySet();
Iterator it=keys.iterator();
while (it.hasNext()){
//迭代器每次返回一个键,通过get方法取出每个键对应的值
System.out.println(map.get(it.next()));
}
System.out.println("===========================");
//增强for循环 foreach
for(Integer key:keys){
System.out.println(key+"="+map.get(key));
}
System.out.println("===========================");
//第二种,将所有键值对转换成一个集合,直接遍历输出
Set<Map.Entry<Integer,String>> kvs=map.entrySet();
//迭代器
Iterator<Map.Entry<Integer,String>> it1=kvs.iterator();
while (it1.hasNext()){
Map.Entry<Integer,String> kv=it1.next();
//Map.Entry<Integer,String>重写了toString方法输出方式: 键=值
System.out.println(kv);
}
System.out.println("===========================");
//foreach
for(Map.Entry<Integer,String> kv:kvs){
System.out.println(kv);
}
}
}
3.HashMap数据结构
- HashMap集合底层是哈希表(散列表)数据结构
- 哈希表
- 哈希表是数组和单向链表的结合体
- 每一个元素都是一个单向链表的头结点
- 哈希表把这两种数据结构融合,发挥两者各自的优点
- 哈希表数组每个单向链表的哈希值是相等的(单向链表就是用来解决哈希冲突的)
- 哈希表的查找就像查字典,先查目录(哈希值),再查具体内容(单链表逐个遍历)
- HashMap底层源码
-
public class HashMap{ //HashMap底层是一个一维数组 Node<K,V>[] table; static class Node<K,V>{ //静态内部类HashMap.Node final int hash;//哈希值 final K key;//存储到HashMap中的key V value;//存储到HashMap中的value Node<K,V> next;//下一个结点的内存地址 } }
- 主要掌握
- map.put(k,v)
- v=map.get(k)
这两个方法的实现原理必须掌握
- 再次分析为什么HashMap无序不重复
- 无序
- 添加的时候位置是任意的,可能在数组上也可能在某个单链表上
- 不重复
- 在添加的时候会调用每个结点的equals方法,如果相同value会覆盖
- 所以HashSet集合中的元素要同时重写hashCode()和equals方法
- HashMap使用不当会无法发挥性能
- 如果hashCode()方法返回的 是固定值,就变成纯单向链表了
- 如果hashCode()方法返回的每个值都不同,就变成纯数组了
- 最好是散列分布均匀的,比如100个元素有十个哈希值(十个单向链表)
- 意味着
- 重写hashCode()要有一定的技巧
- Map中的key部分元素和HashSet中的元素都要同时重写HashCode和equals方法(满足集合查找的需要)
- HashMap集合的默认元素个数是16,默认加载因子是0.75
- 容量达到75%后自动开始扩容
- HashMap初始化容量官方推荐为2的倍数,因为这样可以达到散列均匀,效率更高
import java.util.*;
public class Main {
public static void main(String[] args) {
//创建两个学生对象
Student s1=new Student("zhangsan");
Student s2=new Student("zhangsan");
//未重写equals方法之前
/*
System.out.println(s1.equals(s2));//false,比较的是地址
System.out.println(s1.hashCode());//603742814
System.out.println(s2.hashCode());//1067040082,两个对象的hashCode也不同
*/
//重写equals方法之后
//对象相等意味着hashCode相等,所以hashCode和equals方法应该同时重写
System.out.println(s1.equals(s2));//true
System.out.println(s1.hashCode());//-1432604525
System.out.println(s2.hashCode());//-1432604525,重写后根据学生姓名返回hashCode,姓名一样hashCode一样
Set<Student> s=new HashSet<>();
s.add(s1);
s.add(s2);
System.out.println(s.size());//1,如果不重写,两者hashCode不同,都会存入集合,输出2,不符合集合的不可重复性质
}
}
class Student{
private String name;
public Student(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {//学生姓名相等认为两个对象相等
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return name.equals(student.name);
}
@Override
public int hashCode() {
return Objects.hash(name);//通过学生姓名来计算hashCode,姓名一样hashCode一样
}
}
4.HashMap和Hashtable的区别
- HashMap的key和value都可以是null
- Hashtable的key和value都可以是null
- Hashtable和HashMap底层都是哈希表数据结构
- Hashtable的初始化容量是11,默认加载因子是0.75
- Hashtable默认扩容是 原容量*2+1
- Hashtable是线程安全的,由于对线程的处理,导致效率降低,现在很少用
5.Properties类
- Properties是一个Map类,继承Hashtable
- Properties的key和value都是字符串类型
- Properties被称为属性类对象
- Properties是线程安全的
import java.util.Properties;
public class Main {
public static void main(String[] args) {
Properties p=new Properties();
p.setProperty("1","username");
p.setProperty("2","password");
p.setProperty("3","student");
System.out.println(p.get("1"));//username
System.out.println(p.get("2"));//password
System.out.println(p.get("3"));//student
}
}