我已经阅读了有关Hashmap冲突的内容并采用了以下示例。因为我无法理解什么是碰撞以及如何避免碰撞。示例如下
import java.util.*;
class JavaCollision{
public static void main(String args[]){
HashMap<Person, String> map=new HashMap<Person, String>();
Person p1 = new Person(1,"ABC");
Person p2 = new Person(2,"DEF");
Person p3 = new Person(1,"XYZ");
Person p4 = new Person(1,"PQR");
Person p5 = new Person(1,"PQR");
System.out.println("Adding Entries ....");
map.put(p1,"ONE");
map.put(p2,"TWO");
map.put(p3,"THREE");
map.put(p4,"FOUR");
map.put(p5,"FIVE");
System.out.println("\nComplete Map entries \n" + map);
/* System.out.println("\nAccessing non-collided key");
System.out.println("Value = "+map.get(p2));
System.out.println("\nAccessing collided key");
System.out.println("Value = " + map.get(p1));*/
System.out.println("P1 hashcode is:"+map.get(p1).hashCode()+" And value is:"+map.get(p1));
System.out.println("P2 hashcode is:"+map.get(p2).hashCode()+" And value is:"+map.get(p2));
System.out.println("P3 hashcode is:"+map.get(p3).hashCode()+" And value is:"+map.get(p3));
System.out.println("P4 hashcode is:"+map.get(p4).hashCode()+" And value is:"+map.get(p4));
System.out.println("P5 hashcode is:"+map.get(p5).hashCode()+" And value is:"+map.get(p5));
}
}
Person.java
public class Person {
private int id;
private String name;
public Person(int id, String name) {
this.id = id; this.name = name;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setName (String name) {
this.name = name;
}
public int hashCode(){
System.out.println("Called hashcode for:"+id+" - "+name);
return id;
}
public boolean equals(Object obj){
System.out.println("Called equals on ="+id+" - "+name+" to compare with "+((Person)obj));
boolean result=false;
if(obj instanceof Person){
if( ((Person)obj).getId() == id && ((Person)obj).getName().equals(name) ){
result=true;
System.out.println("Result is true");
}
}
return result;
}
public String toString() {
return id+" . "+name;
}
}
结果是
Adding Entries ....
Called hashcode for:1 - ABC
Called hashcode for:2 - DEF
Called hashcode for:1 - XYZ
Called equals on =1 - XYZ to compare with 1 . ABC
Called hashcode for:1 - PQR
Called equals on =1 - PQR to compare with 1 . ABC
Called equals on =1 - PQR to compare with 1 . XYZ
Called hashcode for:1 - PQR
Called equals on =1 - PQR to compare with 1 . ABC
Called equals on =1 - PQR to compare with 1 . XYZ
Called equals on =1 - PQR to compare with 1 . PQR
Result is true
Complete Map entries
{1 . ABC=ONE, 1 . XYZ=THREE, 1 . PQR=FIVE, 2 . DEF=TWO}
Called hashcode for:1 - ABC
Called hashcode for:1 - ABC
P1 hashcode is:78406 And value is:ONE
Called hashcode for:2 - DEF
Called hashcode for:2 - DEF
P2 hashcode is:83500 And value is:TWO
Called hashcode for:1 - XYZ
Called equals on =1 - XYZ to compare with 1 . ABC
Called hashcode for:1 - XYZ
Called equals on =1 - XYZ to compare with 1 . ABC
P3 hashcode is:79801726 And value is:THREE
Called hashcode for:1 - PQR
Called equals on =1 - PQR to compare with 1 . ABC
Called equals on =1 - PQR to compare with 1 . XYZ
Called hashcode for:1 - PQR
Called equals on =1 - PQR to compare with 1 . ABC
Called equals on =1 - PQR to compare with 1 . XYZ
P4 hashcode is:2158258 And value is:FIVE
Called hashcode for:1 - PQR
Called equals on =1 - PQR to compare with 1 . ABC
Called equals on =1 - PQR to compare with 1 . XYZ
Called equals on =1 - PQR to compare with 1 . PQR
Result is true
Called hashcode for:1 - PQR
Called equals on =1 - PQR to compare with 1 . ABC
Called equals on =1 - PQR to compare with 1 . XYZ
Called equals on =1 - PQR to compare with 1 . PQR
Result is true
P5 hashcode is:2158258 And value is:FIVE
在此结果中,p4和p5具有相同的哈希码。如何避免在hashmap中使用相同的哈希码。我们能否通过返回哈希码来避免冲突。
答案 0 :(得分:1)
您对HashMap
中的碰撞有一种奇怪的理解。它实际上非常简单 - 当两个对象具有相同的哈希码之后在HashMap
内完成内部重新哈希:
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
基本上,在计算哈希码之后,HashMap
将采用前16位,XOR
采用最后16位 - 确保更好的分布。如果在重新散列之后,两个不同的键具有哈希码值 - 这称为哈希冲突。这意味着两个条目将在同一个存储桶中结束。
但通常这是一个性能瓶颈,因为放在同一个存储桶中的条目(达到一定数量后)会被放入perfectly balanced red-black tree
,搜索仍然非常快。例如,如果此存储区中有2 pow 32
条目,则需要最大 32步才能找到此条目;此O(logn)
中的一般搜索时间为Tree bucket
。而对于地图本身,搜索时间将为O(1)
- 不变。
所以不要让自己复杂化 - 虽然你可以测试你的Key#hashCode
以便它有一个更好的哈希码 - 不要这样做,除非你确定有一个性能问题(我非常怀疑)。
答案 1 :(得分:1)
在此结果中,p4和p5具有相同的哈希码。
正确,但这不是问题所在。问题是p4和p5具有相同的键,即id
的值。地图不包含重复项,因此p4被p5覆盖,因为它们具有相同的equals()
,而不仅仅是相同的哈希码。
如何避免在hashmap中使用相同的哈希码。
没有必要避免HashMaps中的冲突,这不是问题所在。
我们能否通过返回哈希码来避免冲突。
是的,但这不是问题所在。如果您想在此地图中同时使用p4和p5,则必须为它们提供不同的键。在这种情况下,这意味着您的id
方法基于{{1} }}和name
,您的hashCode()
方法应该是:
public int hashCode()
{
return id+name.hashCode(); // for example
}