Java HashMap碰撞示例不起作用

时间:2011-08-10 00:37:59

标签: java hashmap collision

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

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

我在这里做错了什么(注意我故意将“count”从hashCode中取出并等于方法)?

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

4 个答案:

答案 0 :(得分:3)

散列映射中的键是散列的,而不是值。在您的情况下,您正在调用put(key, node),并且键是常量,因此第二个put将覆盖第一个{{1}}。如果节点是密钥,那么您将有两个条目。

答案 1 :(得分:2)

你应该看看关键而不是价值。

答案 2 :(得分:1)

如果您阅读了javadoc for HashMap,则会注意到put()方法定义如下

  

put(对象键,对象值)

     

将指定值与此映射中的指定键相关联。

当您使用相同的密钥拨打put()两次时,它会覆盖与该密钥相关联的原始值。

除此之外,该表使用的哈希值而不是值来查找正确的存储桶。

除此之外,你对班级的理解是正确的。

但是,我不相信您可以查看来自公共API的冲突,因为这很可能是在类中处理的。

答案 3 :(得分:0)

这是使用HashMap&lt; Node,Node&gt;的更新代码。我还更改了equals()方法以包含count字段,但将其从hashCode()方法中删除。这成功地创建了一个碰撞,如果你用调试器查看HashMap,你可以看到它在碰撞桶中构建的列表:

public class HashMapTest {

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

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

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

    nodeMap.put(node1, 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"));
    System.out.println("hash map contains node3? " + (nodeMap.containsValue(node3) ? "true" : "false"));
    System.out.println("node1's count from map: " + nodeMap.get(node1).getCount());
    System.out.println("");

    nodeMap.put(node2, 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"));
    System.out.println("hash map contains node3? " + (nodeMap.containsValue(node3) ? "true" : "false"));
    System.out.println("node1's count from map: " + nodeMap.get(node1).getCount());
    System.out.println("");

    // note that if node4 is used then it replaces the value that stored node1
    nodeMap.put(node3, node3);
    System.out.println("added node3 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"));
    System.out.println("hash map contains node3? " + (nodeMap.containsValue(node3) ? "true" : "false"));
    System.out.println("node1's count from map: " + nodeMap.get(node1).getCount());
}

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 + getOuterType().hashCode();
        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 (!getOuterType().equals(other.getOuterType()))
            return false;
        if (count == null) {
            if (other.count != null)
                return false;
        }
        else
            if (!count.equals(other.count))
                return false;
        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;
    }

    private HashMapTest getOuterType() {
        return HashMapTest.this;
    }

}

}

输出:

node1 hash: 1077170390
node2 hash: 1077170391
node3 hash: 1077170390
node1 hash == node2 hash? false
node2 hash == node3 hash? false
node1 hash == node3 hash? true
node1.equals(node2)? false
node2.equals(node3)? false
node1.equals(node3)? 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
hash map contains node3? false
node1's count from map: 1

added node2 to hash map
hash map size: 2
hash map entry set size: 2
hash map contains node1? true
hash map contains node2? true
hash map contains node3? false
node1's count from map: 1

added node3 to hash map
hash map size: 3
hash map entry set size: 3
hash map contains node1? true
hash map contains node2? true
hash map contains node3? true
node1's count from map: 1