10.1 HashSet 的全面说明
package com.xjz.set_;
import java.util.HashSet;
import java.util.Set;
@SuppressWarnings({"all"})
public class HashSet_ {
public static void main(String[] args) {
//1. 构造器走的源码
/*
public HashSet() {
map = new HashMap<>();
}
2/ HashSet 可以存放 null,但是只能有一个null,即元素不能重复
*/
Set hashSet = new HashSet();
hashSet.add(null);
hashSet.add(null);
System.out.println("hashSet=" + hashSet);
}
}
10.2 HashSet 案例说明
package com.xjz.set_;
import java.util.HashSet;
@SuppressWarnings({"all"})
public class HashSet01 {
public static void main(String[] args) {
HashSet set = new HashSet();
//说明
//1. 在执行 add方法后,会返回一个boolean值
//2. 如果添加成功,返回 true,否则返回fasle
//3. 可以通过 remove 指定删除哪个对象
System.out.println(set.add("john"));//T
System.out.println(set.add("lucy"));//T
System.out.println(set.add("john"));//F
System.out.println(set.add("jack"));//T
System.out.println(set.add("Rose"));//T
set.remove("john");
System.out.println("set=" + set); // 3个
set = new HashSet();
System.out.println("set=" + set); // 0个
//4 Hashset 不能添加相同的元素/数据?
set.add("lucy");//添加成功
set.add("lucy");//加入不了
set.add(new Dog("tom"));//OK
set.add(new Dog("tom"));//OK
System.out.println("set=" + set);
//在加深一下,非常经典的面试题
//看源码,做分析
//去看它的源码,即 add 到底发生了什么?==》底层机制
set.add(new String("qdy"));//ok
set.add(new String("qdy"));//加入不了
System.out.println("set=" + set);
}
}
class Dog{ //定义了 Dog 类
private String name;
public Dog(String name) {
this.name = name;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
'}';
}
}
10.3 HashSet 底层机制说明
HashSetStructure.java
package com.xjz.set_;
@SuppressWarnings({"all"})
public class HashSetStructure {
public static void main(String[] args) {
//模拟一个HashSet的底层(HashMap 的底层结构)
//1. 创建一个数组,数组的类型是 Node[]
//2. 有些人,直接把 Node[] 数组成为 表
Node[] table = new Node[16];
System.out.println("table=" + table);
//3. 创建结点
Node john = new Node("john", null);
table[2] = john; // 把 john 放到 table 表的索引为2的位置上
Node jack = new Node("jack", null);
john.next = jack; // 将jack 结点挂载到john
Node rose = new Node("Rose", null);
jack.next = rose; // 将rose 结点挂载到jack
Node lucy = new Node("lucy", null);
table[3] = lucy; // 把lucy 放到 table 表的索引为3的位置
System.out.println("table=" + table);
}
}
class Node { //结点,存储数据,可以指向下一个结点,从而形成链表
Object item; //存放数据
Node next; // 指向下一个结点
//有参构造器
public Node(Object item, Node next) {
this.item = item;
this.next = next;
}
}
HashSetSource.java
package com.xjz.set_;
import java.util.HashSet;
@SuppressWarnings({"all"})
public class HashSetSource {
public static void main(String[] args) {
HashSet hashSet = new HashSet();
hashSet.add("java"); //到此位置,第1次 add 分析完毕.
hashSet.add("php"); //到此位置,第2次 add 分析完毕.
hashSet.add("java");
System.out.println("set=" + hashSet);
/*
对 HashSet 的源码解读
1. 执行 HashSet()
public HashSet() {
map = new HashMap<>();
}
2. 执行 add()
public boolean add(E e) { // e = "java"
return map.put(e, PRESENT)==null; //(static) PRESENT = new Object();
}
3. 执行 put(),该方法会执行 hash(key) 得到 key 对应的 hash 值,算法 h=key.hashCode())^(h>>>16)
public V put(K key, V value) { //key = "java" value = PRESENT 共享
return putVal(hash(key), key, value, false, true);
}
4. 执行 putVal()
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i; //定义了辅助变量
//table 就是 HashMap 的一个数组,类型是 Node[]
//if 语句表示如果当前 table 是 null,或者大小=0
//就是第一次扩容,到16个空间
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//(1)根据 key,得到 hash 去计算该 key 应该存放到 table 表的哪个索引位置
//并把这个位置的对象,赋给p
//(2)判断 p 是否为null
//(2.1) 如果 p 为 null,表示还没有存放元素,则创建了一个 Node(key="java",value=PRESENT)
//(2.2) 就放在该位置 tab[i] = newNode(hash,key,value,null)
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
//一个开发技巧提示:在需要局部变量(辅助变量)时候,再创建它
Node<K,V> e; K k;
//如果当前索引位置对应的链表的第一个元素和准备添加的 key 的 hash 值一样
//并且满足 下面两个条件之一:
//(1) 准备加入的 key 和 p 指向的 Node 结点的 key 是同一个对象
//(2) p 指向的 Node 结点的 key 的 equals() 和准备加入的 key 比较后相同
// 就不能加入
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//再判断 p 是不是一棵红黑树.
//如果是一棵红黑树,就调用 putTreeVal,来进行添加
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else { //如果 table 对应索引位置,已经是一个链表,就使用 for 循环比较
//(1) 依次和该链表的每一个元素比较后,都不相同,则加入到该链表的最后
// 注意在把元素添加到链表后,立即判断 该链表是否已经达到 8 个结点
// ,就调用 treeifyBin() 对当前这个链表进行树化(转成红黑树)
// 注意,在转成红黑树时,要进行判断,判断条件
// if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY(64))
// resize();
// 如果上面条件成立,先 table 扩容
// 只有上面条件不成立时,才进行转成红黑树
//(2) 依次和该链表的每一个元素比较过程中,如果有相同情况,就直接 break
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
// size 就是我们每加入一个结点 Node(k,v,h,next),size++
if (++size > threshold)
resize(); // 扩容
afterNodeInsertion(evict);
return null;
}
*/
}
}
HashSetIncrement.java
package com.xjz.set_;
import java.util.HashSet;
import java.util.Objects;
@SuppressWarnings({"all"})
public class HashSetIncrement {
public static void main(String[] args) {
/*
HashSet 底层是 HashMap, 第一次添加时,table 数组扩容到 16,
临界值(threshold)是 16*加载因子(loadFactor)是 0.75 = 12
如果 table 数组使用到了临界值 12,就会扩容到 16 * 2 = 32,
新的临界值就是 32*0.75 = 24, 依次类推
*/
HashSet hashSet = new HashSet();
// for(int i = 1;i <= 100; i++){
// hashSet.add(i); //1,2,3,4,5....100
// }
/*
在 Java8 中, 如果一条链表的元素个数到达 TREEIFY_THRESHOLD(默认是 8 ),
并且 table 的大小 >= MIN_TREEIFY_CAPACITY(默认 64),就会进行树化(红黑树),
否则仍然采用数组扩容机制
*/
// for(int i = 1; i <= 12; i++){
// hashSet.add(new A(i)); //
// }
/*
当我们向 hashset 增加一个元素,-> Node -> 加入 table , 就算是增加了一个 size++
*/
for (int i = 1;i <= 7; i++){ //在 table 的某一条链表上添加了 7 个 A 对象
hashSet.add(new A(i));
}
for (int i = 1;i <=7; i++){ //在 table 的某一条链表上添加了 7 个 B 对象
hashSet.add(new B(i));
}
}
}
class B{
private int n;
public B(int n) {
this.n = n;
}
@Override
public int hashCode() {
return 200;
}
}
class A{
private int n;
public A(int n) {
this.n = n;
}
@Override
public int hashCode() {
return 100;
}
}
10.4 HashSet 课堂练习1
HashSetExercise.java
package com.xjz.set_;
import java.util.HashSet;
import java.util.Objects;
@SuppressWarnings({"all"})
public class HashSetExercise {
public static void main(String[] args) {
/**
定义一个 Employee 类,该类包含:private 成员属性 name,age 要求:
创建 3 个 Employee 对象放入 HashSet 中
当 name 和 age 的值相同时,认为是相同员工, 不能添加到 HashSet 集合中
*/
HashSet hashSet = new HashSet();
hashSet.add(new Employee("milan",18));//ok
hashSet.add(new Employee("smith",28));//ok
hashSet.add(new Employee("milan",18));//加入不成功
//回答,如果不重写equals方法 加入了几个? 3个
System.out.println("hashSet=" + hashSet);
}
}
//创建 Employee
class Employee {
private String name;
private int age;
public Employee(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 "Employee{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return age == employee.age &&
Objects.equals(name, employee.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
10.5 HashSet 课后练习2
只需要把 Employee01 和 MyDate 两个类都重写一下 equals( ) 和 hashCode( ) 方法,就不会有相同员工进入
package com.xjz.set_;
import java.util.HashSet;
import java.util.Objects;
@SuppressWarnings({"all"})
public class HashSetExercise02 {
public static void main(String[] args) {
//只需要把 Employee01 和 MyDate 两个类都重写一下 equals() 和 hashCode() 方法,就不会有相同员工进入
HashSet hashSet = new HashSet();
hashSet.add(new Employee01("秋刀鱼", 19, new MyDate(2002, 12, 02)));
hashSet.add(new Employee01("苏炳添", 32, new MyDate(1989, 04, 21)));
hashSet.add(new Employee01("秋刀鱼", 19, new MyDate(2002, 12, 02)));
System.out.println("hashSet=" + hashSet);
}
}
//创建Employee类
class Employee01 {
private String name;
private int sal; //薪水
private MyDate birthday;
public Employee01(String name, int sal, MyDate birthday) {
this.name = name;
this.sal = sal;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Employee01{" +
"name='" + name + '\'' +
", sal=" + sal +
", birthday=" + birthday +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee01 that = (Employee01) o;
return sal == that.sal &&
Objects.equals(name, that.name) &&
Objects.equals(birthday, that.birthday);
}
@Override
public int hashCode() {
return Objects.hash(name, sal, birthday);
}
}
class MyDate {
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public String toString() {
return "MyDate{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyDate myDate = (MyDate) o;
return year == myDate.year &&
month == myDate.month &&
day == myDate.day;
}
@Override
public int hashCode() {
return Objects.hash(year, month, day);
}
}package com.xjz.set_;
import java.util.HashSet;
import java.util.Objects;
@SuppressWarnings({"all"})
public class HashSetExercise02 {
public static void main(String[] args) {
//只需要把 Employee01 和 MyDate 两个类都重写一下,就不会有相同员工进入
HashSet hashSet = new HashSet();
hashSet.add(new Employee01("秋刀鱼", 19, new MyDate(2002, 12, 02)));
hashSet.add(new Employee01("苏炳添", 32, new MyDate(1989, 04, 21)));
hashSet.add(new Employee01("秋刀鱼", 19, new MyDate(2002, 12, 02)));
System.out.println("hashSet=" + hashSet);
}
}
//创建Employee类
class Employee01 {
private String name;
private int sal; //薪水
private MyDate birthday;
public Employee01(String name, int sal, MyDate birthday) {
this.name = name;
this.sal = sal;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Employee01{" +
"name='" + name + '\'' +
", sal=" + sal +
", birthday=" + birthday +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee01 that = (Employee01) o;
return sal == that.sal &&
Objects.equals(name, that.name) &&
Objects.equals(birthday, that.birthday);
}
@Override
public int hashCode() {
return Objects.hash(name, sal, birthday);
}
}
class MyDate {
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public String toString() {
return "MyDate{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyDate myDate = (MyDate) o;
return year == myDate.year &&
month == myDate.month &&
day == myDate.day;
}
@Override
public int hashCode() {
return Objects.hash(year, month, day);
}
}