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

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

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

问题

1
2
3
4
5
6
7
8
9
//保证了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方法有关系,于是做了一个小测试

1
2
3
4
5
6
7
8
9
10
11
12
//简单的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方法(右键自动生成的。。。)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@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


看源码

一层一层的找往里找

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

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

1
2
3
4
//HashMap
public boolean containsKey(Object key) {
return getNode(hash(key), key) != null;
}
1
2
3
4
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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类
尝试看一下两个对象的哈希值是否一样

1
2
3
4
5
6
7
car a=new car("car1");
car b=new car("car1");
System.out.println(b.hashCode());
System.out.println(a.hashCode());
//结果
1311053135
2018699554

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

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

再来测试car类

1
2
3
4
5
6
7
8
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方法即可。

#
You forgot to set the qrcode for Alipay. Please set it in _config.yml.
You forgot to set the qrcode for Wechat. Please set it in _config.yml.
You forgot to set the business and currency_code for Paypal. Please set it in _config.yml.
You forgot to set the url Patreon. Please set it in _config.yml.
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×