Note that there are some explanatory texts on larger screens.

plurals
  1. POJava HashMap Collision Example Not Working
    primarykey
    data
    text
    <p>My understanding is that Java's implementation of HashMap uses "buckets" which point to a list of values to handle collisions and that an object will only ever be overridden if the hashCode() for the key AND the equals() for the object are BOTH the same for the object being added and the object it collides with.</p> <p>I tried playing around with HashMap to try to see the collision behavior in action but no matter what I do, it always seems to override.</p> <p>What am I doing wrong here (note I intentionally left "count" out of the hashCode and equals methods)?</p> <pre><code>public class HashMapTest { public static void main(String[] args) { HashMapTest test = new HashMapTest(); test.execute(); } public void execute() { HashMap&lt;String, Node&gt; nodeMap = new HashMap&lt;String, Node&gt;(); 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; } } </code></pre> <p>}</p> <p>It outputs:</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload