Java中详述HashSet类add方法(四)

new 对象(“数据内容”)数据重复(二)

在重复数据一中我们发现:new 对象 (“数据内容”)可以重复存储的原因在于存储相同数据时的两个对象的hashCode值不同导致存储地址不同,若重写HashCode方法,探究该情况是否能够发生改变。代码如下:

package sun;

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")));
		System.out.println(set.add(new Student("100")));
		System.out.println(new Student("100").hashCode());
		System.out.println(new Student("100").hashCode());
	}
}
结果为:
true
true
48625
48625
package sun;

public class Student {

	private String id;

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

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

由上面代码可以发现,虽然hashCode的值相同,但是还是能够重复存储相同的数据,这是为什么呢?分析底层源代码:
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);

其调用的hash方法中调用的HashCode方法(调用Student类中重写的方法)为:

static final int hash(Object key) {
     int h;
     return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}

因为前后两次存储数据时的hashCode相同,所以hash(key)的值也相同。

HashMap 中的putVal方法:

static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

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)
            n = (tab = resize()).length;
        if ((p = tab[i = (n - 1) & hash]) == null)
            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))))
                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)// onlyIfAbsent本来就为false,所以!onlyIfAbsent true
                    e.value = value;
                afterNodeAccess(e);
                return oldValue; 
            }
        }
        ++modCount;
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }

摘录除每一步代码进行分析:

static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

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)
            n = (tab = resize()).length;

由于存第一个对象时,全局变量table已经被赋值,所以tab=table不为空,即不等于null。而且n=tab.length=16!=0,因此,if的判断结果为false,进入下一个判断语句。

  		if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null); 
  		else {
        	……
        }

i = (n - 1) & hash 数组长度-1 & hash值,即16-1 & hash,因为第一个和第二个的hash方法返回值相同,所以存第一个值和存第二个值时的(n - 1) & hash值是相同的 ,所以存第二个对象时tab[i = (n - 1) & hash]不为null,进入else中的if判断语句,代码分析:

			Node<K,V> e; K k;
            if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k))))
                e = p;

p.hash(第一个学生id hash的值等于第二个学生id的hash值),所以p.hash= =hash判定为true,而(k = p.key) == key因为p.key为第一次存储时学生id的key,与第二次待存储的学生id的key不同,所以判定为false,(key != null && key.equals(k)) ,因为学生类没有重写equals,所以调用Object类中equals方法,比较的是key与k=p.key的地址,两者地址不同,因此结果为false,进入else if判断语句:

			else if (p instanceof TreeNode)
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            else {
            	……
            }

很显然,p不满足p instanceof TreeNode 语句判断,因此为false,进入else语句:

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 = p.next) == null) {
                        p.next = newNode(hash, key, value, null);

因为e=p.next指向第一次存储位置后的下一个位置,为null因此判定成功,p.next存储第二次代码,因此数据存储成功。因此返回值为null

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

putVal方法得到的返回值为null,因此返回null至上一级put方法;

	public boolean add(E e) {//E e为输入的"wjq"
        return map.put(e, PRESENT)==null;
	}

return map.put(e, PRESENT)==null; 成立,结果为true,因此,返回值为true,数据存储成功。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值