4、详述HashSet类add方法(四)

重写hashcode方法后的元素存储

package sun;
这里重写了Student中的Hashcode方法
public class Student {
	
	private String id;

	public Student(String id) {
		this.id = id;
	}

	@Override
	public int hashCode() {
		return id.hashCode();
	}
}

这里存储不同地址的但经过重写Hashcode方法后hash值相同学生对象,代码解析

package sun;

import java.util.HashSet;

public class Test1 {
	
	public static void main(String[] args) {
		HashSet<Student> set = new HashSet<Student>();
		这里存储不同地址的但经过重写Hashcode方法后hash值相同学生对象
		set.add(new Student("100"));
		set.add(new Student("100"));
	}
}

HashSet add

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

HashSet put

public V put(K key, V value) {
	return putVal(hash(key), key, value, false, true);  存第一个和第二个值的时候由于都是调用Student类中重写后的hashCode方法,该方法返回id的hashCode值;因为id都相同,所以第二次存的对象与第一次存的对象hash(key)结果是一样的
	/*多态,调用Student类中的hashCode()
	static final int hash(Object key) {
     int h;
     return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);//多态,调用Student类中的hashCode()
}*/

HashSet 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,多以该iffalse   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   该iffalse
            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  (p是第一个学生对象,p.hash为第一个学生对象的hash值,hash为第二个学生对象的值,由于存储的id一样,所以为true(k = p.key) == key  false(p.key是第一个学生对象,key是第二个,因为是两个不同的对象,所以为false(key != null && key.equals(k))  因为学生类没有重写equals,所以调用Object类中equals 比较的是地址  false
                e = p;
            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;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue; 
            }
        }
        ++modCount;
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值