java关于集合类的一点点探索

对set集合的contains函数的运行机制有一点点小疑问以及探索

问题

//保证了a,b指向两个ID不同,内容相同的字符串
String a=new String("v1");
String b=new String("v1");
HashSet<String> set=new HashSet<String>();
set.add(a);
System.out.println(set.contains(b));

//结果
true

a,b两个String类指向的对象,id不同,内容相同,contains是如何判断这两个对象相同的?
最开始以为是和equals方法有关系,于是做了一个小测试

//简单的car类
public class car {
	private String name;
}
//main
HashSet<car> s=new HashSet<car>();
car a=new car("car1");
car b=new car("car1");
s.add(a);
System.out.println(s.contains(b));
//结果
false

结果显然是false
于是重写了car的equals方法(右键自动生成的。。。)

@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	car other = (car) obj;
	if (name == null) {
		if (other.name != null)
			return false;
	} else if (!name.equals(other.name))
		return false;
	return true;
}

但是运行main函数仍然显示false


看源码

一层一层的找往里找

private transient HashMap<E,Object> map;
//HashSet的contains方法
public boolean contains(Object o) {
        return map.containsKey(o);
    }

(害!HashSet其实是用HashMap实现的啊)

//HashMap
public boolean containsKey(Object key) {
        return getNode(hash(key), key) != null;
    }
static final int hash(Object key) {
    int h;
    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
final Node<K,V> getNode(int hash, Object key) {
    Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
    if ((tab = table) != null && (n = tab.length) > 0 &&
        (first = tab[(n - 1) & hash]) != null) {
        if (first.hash == hash && // always check firstnode
            ((k = first.key) == key || (key != null &&key.equals(k))))
            return first;
        if ((e = first.next) != null) {
            if (first instanceof TreeNode)
                return ((TreeNode<K,V>)first).getTreeNod(hash, key);
            do {
                if (e.hash == hash &&
                    ((k = e.key) == key || (key != null & key.equals(k))))
                    return e;
            } while ((e = e.next) != null);
        }
    }
    return null;
}

最终getNode方法里面找到了答案(4,5行)
1.判断两个对象hashcode是否相同,
2.判断两个对象是否“相同” (a==b||a.equals(b))


解决问题

回头再来看car类
尝试看一下两个对象的哈希值是否一样

car a=new car("car1");
car b=new car("car1");
System.out.println(b.hashCode());
System.out.println(a.hashCode());
//结果
1311053135
2018699554

所以问题就在此,重写一下hashcode方法试试(之前已经重写了equals方法)

@Override
public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + ((name == null) ? 0 : name.hashCode());
	return result;
}

再来测试car类

System.out.println(b.hashCode());
System.out.println(a.hashCode());
s.add(a);
System.out.println(s.contains(b));
//结果
3046140
3046140
true

总结

HashSet的contains判断两对象相同的方法:
1.判断两个对象hashcode是否相同,
2.判断两个对象是否“相同” (a==b||a.equals(b))

所以!为正在使用的类重写hashCode和equals方法即可。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值