Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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 ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Hashmap collision

 
Greenhorn
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.
 
Bartender
Posts: 4179
22
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).
 
Hot dog! An advertiser loves us THIS much:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic