Details the add method of the HashSet class (5)

Override the equals method in a custom class

public class Student {
    private String 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 ){
            Student stu = (Student)obj;
            return this.id.equals(stu.id);
        }
        return false;
    }
}

 

import java.util.HashSet;

public class Test1 {

    public static void main(String[] args) {
        HashSet<Student> set = new HashSet<Student>();
        System.out.println(set.add(new Student("100")));//true
        System.out.println(set.add(new Student("100")));//false
    }

}

Add method in HashSet

public boolean add(E e) {
        return map.put(e, PRESENT)==null;
    }

Put method in HashMap

public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }

PutVal method in HashMap

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {         Node<K,V>[] tab; Node<K,V> p; int n, i;         if ((tab = table) == null || (n = tab.length) == 0)//The second table is not empty             n = (tab = resize()).length; //do not execute         if ((p = tab(i = (n-1) & hash]) == null) //hash value is the same             tab[i] = newNode(hash, key, value, null); //do not execute         else {             Node<K,V> e; K k ;             if (p.hash == hash &&                 ((k = p.key) == key || (key != null && key.equals(k)))) //Execute the rewritten equals method, return value Is true









                e = p;                                                                                      //e被辅赋值
            else if (p instanceof TreeNode)
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            else {
                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                      //e不为空
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;                                           //putVal返回值不为空
            }
        }
        ++modCount;
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }

put return value is not empty

add method returns false, add failed

 

 

 

 

Guess you like

Origin blog.csdn.net/SignalFire/article/details/105747809