详述HashSet类add方法(五)(自定义类重写equals和hashCode()方法)

当进行如下代码:

package sun;

public class Student {
	
	private String id;

	public Student(String id) {
		this.id = id;
	}
	@Override
	public boolean equals(Object obj) {//obj值集合中的某个对象,因为集合中可能存的未必都是学生类,比如集合泛型为Object的时候,既可以存学生对象,还可以存其它对象
		if(obj instanceof Student ){//需要先判断是否存储的是Student类的,然后才能存储
			Student stu = (Student)obj;
			return this.id.equals(stu.id);//this即是当前正在试图被存储的对象
		}
		return false;
	}
	@Override
	public int hashCode() {
		return id.hashCode();//重写了hashCode()方法
	}
}
package sun;

import java.util.HashSet;

public class Test1 {
	
	public static void main(String[] args) {
		HashSet<Student> set = new HashSet<Student>();
		set.add(new Student("100"));
		set.add(new Student("100"));
	}
}

HashSet add

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

HashMap put方法

public V put(K key, V value) {
	return putVal(hash(key), key, value, false, true);//存第一个和第二个值的时候由于都是调用Student类中重写后的hashCode方法,该方法返回id的hashCode值;因为id都相同,所以第二次存的对象与第一次存的对象hash(key)结果是一样的

HashMap 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;
        if ((tab = table) == null || (n = tab.length) == 0)//由于存第一个对象时table全局变量已经有值了,所以tabl  tab不为空   tab.length不等于0,多以该if为false   n为 16
            n = (tab = resize()).length;
        if ((p = tab[i = (n - 1) & hash]) == null)// i = (n - 1) & hash 数组长度-1  & hash值,即16-1 & hash,因为第一个和第二个值的hash方法返回值相同,所以存第一个值和存第二个值的时候(n - 1) & hash的值是一样的 ,所以存第二个对象时tab[i = (n - 1) & hash]不为null p变量存储的是集合中已有元素
            tab[i] = newNode(hash, key, value, null); 
        else {
            Node<K,V> e; K k;
            if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k))))// p.hash == hash true   (k = p.key) == key  false   (key != null && key.equals(k))  因为学生类重写equals,所以调用Student类中equals 比较的是两个对象id是否相同  true
                e = p;//将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
                V oldValue = e.value; // e.value  常量Object
                if (!onlyIfAbsent || oldValue == null)// onlyIfAbsent本来就为false,所以!onlyIfAbsent为 true
                    e.value = value;//value变量其实是第二次存储对象时存入的值 Object(PRESENT)
                afterNodeAccess(e);
                return oldValue; //返回第一个学生对象,存储失败
            }
        }
        ++modCount;
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);

上一篇:详述HashSet类add方法(四)(自定义类重写hashCode()方法)

发布了28 篇原创文章 · 获赞 0 · 访问量 414

猜你喜欢

转载自blog.csdn.net/syhfly2333/article/details/105745292