• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Hashmap collision

Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

I am a bit confused about hashmap collision and resulting performance degradation. I did a test to determine collision:

public class ObjectToStore {
String value = null;
public ObjectToStore(String val){
value = val;


public boolean equals(Object o){

if (o.hashCode() == hashCode()){
return true;
return false;
public String getValue(){
return value;
public int hashCode(){
return 1;


package com.nm.main;

import java.util.HashMap;

public class HashmapTest {

public static void main(String[] args){
ObjectToStore a1 = new ObjectToStore("a");
ObjectToStore b1 = new ObjectToStore("b");
ObjectToStore c1 = new ObjectToStore("c");
ObjectToStore d1 = new ObjectToStore("d");

HashMap<ObjectToStore, ObjectToStore> map = new HashMap<ObjectToStore, ObjectToStore>();
map.put(a1, a1);
map.put(b1, b1);
map.put(c1, c1);
map.put(d1, d1);

ObjectToStore a12 = map.get(a1);
ObjectToStore b12 = map.get(b1);
ObjectToStore c12 = map.get(c1);
ObjectToStore d12 = map.get(d1);

System.out.println("Should print a: " + a12.getValue());
System.out.println("Should print b: " + b12.getValue());
System.out.println("Should print c: " + c12.getValue());
System.out.println("Should print d: " + d12.getValue());



Result is:
Should print a: d
Should print b: d
Should print c: d
Should print d: d

Which shows there is actually no collision and hashmap just straight way overrides the values for keys with same hashcode. Is my assumption correction? Or I am missing any point?

Thanks in advance for any input.
Posts: 4179
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No,that is not correct. If the hash codes are the same, the objects are put in the same bucket and then compared to other objects in the bucket using .equals. If the .equals returns true the value is replaced.

In your test, your .equals comparison returns true any time the hash code is the same, so the object will always be replaced. But that isn't always the case (objects that are equal must have the same hash code, but objects with the same hash code do not have to be equal).
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic