HashSet集合中remove()方法删除自定义类型对象的执行过程分析
注意:此处的remove方法删除自定义类型对象的执行过程分析是在之前博客add方法存储自定义类型对象的基础上,即Student类中已经重写了hashCode方法和equals方法,原因此处也不再赘述。详见详述HashSet集合中add()方法存储自定义类型对象的执行过程
下面是自定义类代码:
public class Student{
String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Student(String id) {
this.id = id;
}
@Override
public int hashCode() {
return id.hashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Student)) {
throw new ClassCastException("类型转换错误!");
}
Student student = (Student) obj;
return this.id.equals(student.id);
}
}
下面是测试代码:
HashSet<Student> hashSet = new HashSet<>();
hashSet.add(new Student("1"));
hashSet.add(new Student("2"));
System.out.println(hashSet);
hashSet.remove(new Student("1"));
System.out.println(hashSet);
下面是运行结果截图:
由于在上一篇博客中已详细分析过remove()方法删除String类型对象的执行过程,而remove()方法删除自定义类型对象的执行过程与之类似,此处不再重复叙述,只将不同之处做详细描述。下面是截取的HashMap类中的removeNode方法的代码:
//HashMap类中的removeNode方法
final Node<K,V> removeNode(int hash, Object key, Object value,
boolean matchValue, boolean movable) {
Node<K,V>[] tab; Node<K,V> p; int n, index;
if ((tab = table) != null && (n = tab.length) > 0 &&
(p = tab[index = (n - 1) & hash]) != null) {
Node<K,V> node = null, e; K k; V v;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
node = p;
else if ((e = p.next) != null) {
if (p instanceof TreeNode)
node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
else {
do {
if (e.hash == hash &&
((k = e.key) == key ||
(key != null && key.equals(k)))) {
node = e;
break;
}
p = e;
} while ((e = e.next) != null);
}
}
if (node != null && (!matchValue || (v = node.value) == value ||
(value != null && value.equals(v)))) {
if (node instanceof TreeNode)
((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
else if (node == p)
tab[index] = node.next;
else
p.next = node.next;
++modCount;
--size;
afterNodeRemoval(node);
return node;
}
}
return null;
}
执行过程分析:
1、代码hashSet.add(new Student(“1”))的执行过程:在之前的博客中已详细分析过该执行过程,此处不再赘述,只是稍加补充。当执行tab[i] = newNode(hash, key, value, null)语句时,此处的key是Student对象的地址,而value则是在HashSet类中定义的PRESENT常量,每添加一个Student类型的对象,就会对应一个唯一的PRESENT常量值。
//HashSet类中定义的PRESENT常量
private static final Object PRESENT = new Object();
//HashSet类中的add方法
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
2、代码hashSet.remove(new Student(“1”))的执行过程:
(1).程序执行 if ((tab = table) != null && (n = tab.length) > 0
&&(p = tab[index = (n - 1) & hash]) != null) 语句,此处的tab(table)指向的数组中已经存储了添加的Student对象,而p指向的地址是存储new Student(“1”)对象的地址,也不为空,故条件成立,执行if代码块中的内容。
(2).程序执行 if (p.hash == hash &&((k = p.key) == key || (key != null && key.equals(k))))
语句,p.hash == hash语句实质是在比较tab(table)指向的数组中存储的new Student(“1”)对象的hash值和本次传入的欲删除的Student对象的hash值,由之前的分析可知为true,但是(k = p.key) == key中,k=p. key即k和p中存储的都是数组中存储new Student(“1”)对象的地址,而key是本次传入的欲删除的Student对象的地址,显然不同,故而执行key != null && key.equals(k)语句,key!=null成立执行key.equals(k),由于Student类中已经重写了Object类中的equals方法,故而key.equals(k)结果为true,条件成立,执行node = p。
(3).程序执行if (node != null && (!matchValue || (v = node.value) == value ||(value != null && value.equals(v))))语句,node!=null条件成立且!matchValue 为true,故执行if代码块中的内容。
(4).程序执行 if (node instanceof TreeNode)((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);语句,由于node声明的时候属于Node类,但是Node和TreeNode没有子父类关系,所以程序执行else if代码块中的内容。又因为上面把p赋给了node所以这里node==p返回true,程序执行tab[index] = node.next;语句,把node后面的元素赋给当前数组索引的位置,node对象中并没有给next赋值,所以这里相当于把数据当前索引的位置置为空,删除成功。return node,即返回之前tab(table)数组中添加的"b"对象地址。
(5).程序执行HashMap类中的remove方法,执行return (e = removeNode(hash(key), key, null, false, true)) == null?null : e.value;语句,removeNode方法返回值不为空,故e指向的是之前tab(table)指向的数组中添加的new Student(“1”)对象地址,不为空,故返回结果为e.value,而e.value的值就是之前添加new Student(“1”)对象时对应的PRESENT常量值。
(6).程序执行HashSet类中的remove方法,返回true,删除成功。