- HashMap(重点):
- jdk1.2版本,线程不安全,运行效率快;允许用null作为key或是value
- 存储结构:哈希表(数组+链表+红黑树)
- 源码分析:
- hashMap初始容量大小:static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
- hashMap的数组最大容量:static final int MAXIMUM_CAPACITY = 1 << 30;
- 默认加载因子:static final int DEFAULT_LOAD_FACTOR = 0.75f;
- jdk1.8当链表长度大于8时,调整为红黑树:static final int TREEIFY_THRESHOLD = 8;
- jdk1.8当链表长度小于6时,调整为链表:static final int UNTREEIFY_THRESHOLD = 6;
- jdk1.8当链表长度大于8时,并且集合元素个数大于等于64时,调整为红黑树:static final int MIN_TREEIFY_CAPACITY = 64;
- 哈希表中的数组:transient Node<K,V>[] table;
- 元素个数:size;
- 总结:
- HashMap刚创建的时候,table是null,size是0,为了节省空间,当添加第一个元素时,table容量调整为16
- 当元素个数大于阈值(16*0.75=12)时,会进行扩容,扩容后大小是原来的2倍,目的是减少调整元素的个数
- jdk1.8之前,链表是头插入;jdk1.8之后,链表是尾插入
//学生类
public class Student{
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
}
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 hashCode() {
//31是一个质数,减少散列冲突
//31提高执行效率 31*i=(i<<5)-i
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
//HashMap的使用
public class Test{
public static void main(String[] args){
HashMap<Student,String> students = new HashMap<Student,String>();
//添加元素
Student s1 = new Student("张三",20);
Student s2 = new Student("李四",21);
Student s3 = new Student("王五",22);
students.put(s1,"北京");
students.put(s2,"上海");
students.put(s3,"南京");
students.put(new Student("王五",22),"南京"); //重写hashcode和equals无法添加
System.out.println("元素个数:" + students.size()); //元素个数:3
System.out.println(students.toString()); //{Student [name=李四, age=21]=上海, Student [name=王五, age=22]=南京, Student [name=张三, age=20]=北京} 无序的
//遍历
//使用keySet()
for(Student key : students.keySet()){
System.out.println(key.toString() + ":" + students.get(key));
}
//使用entrySet()
for(Map.Entry<Student,String> entry : students.entrySet()){
System.out.println(entry.getKey() + ":" + entry.getValue());
}
//判断
System.out.println(map.containsKey(s1)); //true
System.out.println(map.containsValue("武汉")); //false
//删除
students.remove(s1);
System.out.println("删除之后:" + students.size()); //删除之后:2
}
}
-
HashTable(基本不用了):
- jdk1.0版本,线程安全的,运行速率慢;不允许null作为key和value
- Properties:
- HashTable的子类,要求key和value都是String。通常用于配置文件的读取
-
TreeMap:
- 实现了SortedMap接口(Map的子接口),可以对key自动排序
//创建集合(定制比较)
TreeMap<Student,String> treeMap = new Tree<Student,String>(new Comparator<Student>(){
//先比年龄,后比姓名
@Override
public int compare(Student o1,Student o2){
int n1 = o1.getAge() - o2.getAge();
int n2 = o1.getName.compareTo(o2.getName);
return n1==0?n2:n1;
}
});
//添加元素
Student s1 = new Student("张三",20);
Student s2 = new Student("李四",21);
Student s3 = new Student("王五",22);
treeMap.put(s1,"北京");
treeMap.put(s2,"上海");
treeMap.put(s3,"南京");
//遍历
//keySet
for(Student key : treeMap.keySet()){
System.out.println(key.toString() + ":" + treeMap.get(key));
}
//entrySet
for(Map.Entry<Student,String> entrySet : treeMap.entrySet()){
System.out.println(entrySet.getKey() + ":" + entrySet.getValue());
}
//判断
System.out.println(treeMap.containsKey(s1)); //true
System.out.println(treeMap.containsValue("贵阳")); //false
//删除
treeMap.remove(s1);