gpt4 book ai didi

Java HashMap 冲突示例不工作

转载 作者:行者123 更新时间:2023-12-01 16:38:15 25 4
gpt4 key购买 nike

我的理解是,Java 的 HashMap 实现使用“桶”,它指向一个值列表来处理冲突,并且只有当键的 hashCode() 和对象的 equals() 时,对象才会被覆盖对于要添加的对象和与其碰撞的对象来说都是相同的。

我尝试使用 HashMap 来尝试查看实际的碰撞行为,但无论我做什么,它似乎总是会被覆盖。

我在这里做错了什么(注意我故意将“count”排除在 hashCode 和 equals 方法之外)?

public class HashMapTest {

public static void main(String[] args) {
HashMapTest test = new HashMapTest();
test.execute();
}

public void execute() {
HashMap<String, Node> nodeMap = new HashMap<String, Node>();
Node node1 = new Node("data1", 1);
Node node2 = new Node("data2", 2);

String key = "1";

System.out.println("node1 hash: " + node1.hashCode());
System.out.println("node2 hash: " + node2.hashCode());
System.out.println("node1 hash == node2 hash? " + (node1.hashCode() == node2.hashCode() ? "true" : "false"));
System.out.println("node1.equals(node2)? " + (node1.equals(node2) ? "true" : "false"));

nodeMap.put(key, node1);
System.out.println("added node1 to hash map");
System.out.println("hash map size: " + nodeMap.size());
System.out.println("hash map entry set size: " + nodeMap.entrySet().size());
System.out.println("hash map contains node1? " + (nodeMap.containsValue(node1) ? "true" : "false"));
System.out.println("hash map contains node2? " + (nodeMap.containsValue(node2) ? "true" : "false"));

nodeMap.put(key, node2);
System.out.println("added node2 to hash map");
System.out.println("hash map size: " + nodeMap.size());
System.out.println("hash map entry set size: " + nodeMap.entrySet().size());
System.out.println("hash map contains node1? " + (nodeMap.containsValue(node1) ? "true" : "false"));
System.out.println("hash map contains node2? " + (nodeMap.containsValue(node2) ? "true" : "false"));
}

protected class Node {

private String data;

private Integer count;

public Node(String data, Integer count) {
this.data = data;
this.count = count;
}

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

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


public String getData() {
return data;
}


public void setData(String data) {
this.data = data;
}


public Integer getCount() {
return count;
}


public void setCount(Integer count) {
this.count = count;
}

}

}

它输出:

node1 hash: 95356390
node2 hash: 95356391
node1 hash == node2 hash? false
node1.equals(node2)? false
added node1 to hash map
hash map size: 1
hash map entry set size: 1
hash map contains node1? true
hash map contains node2? false
added node2 to hash map
hash map size: 1
hash map entry set size: 1
hash map contains node1? false
hash map contains node2? true

最佳答案

HashMap 中的键被哈希,而不是值。在您的情况下,您正在调用 put(key, node) ,并且 key 是常量,因此第二个 put 将覆盖第一个。如果节点是键,那么您将有两个条目。

关于Java HashMap 冲突示例不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7004684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com