Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the default hashcode() in java bad?

if I remember correctly the default hashCode() implementation in java of an object of type Object() is to return the memory address of the object. When we create our own classes, I read that we want to override hashCode() so that when we insert them into a hash related collection like HashMap() it will work properly. But why is a memory address bad?

Sure we will EVENTUALLY run out of memory and you'll have collisions, but the only case where I see this being a problem is where you are dealing with TONS of data and have very little memory, and then it would START to affect performance because hash related collections in java resolve collisions by chaining(a bucket will link to a list of values that resolved to the same hashcode/index).

like image 713
HukeLau_DABA Avatar asked Apr 12 '13 18:04

HukeLau_DABA


People also ask

What is default hashCode in Java?

The purpose of the hashCode() method is to provide a numeric representation of an object's contents so as to provide an alternate mechanism to loosely identify it. By default the hashCode() returns an integer that represents the internal memory address of the object.

What happens if we do not override hashCode () and equals () in HashMap?

If you don't override hashcode() then the default implementation in Object class will be used by collections. This implementation gives different values for different objects, even if they are equal according to the equals() method.

Is hashCode always unique in Java?

Hashcode is a unique code generated by the JVM at time of object creation. It can be used to perform some operation on hashing related algorithms like hashtable, hashmap etc. An object can also be searched with this unique code. Returns: It returns an integer value which represents hashCode value for this Method.

What happens if we override hashCode in Java?

Only Override HashCode, Use the default Equals: Only the references to the same object will return true. In other words, those objects you expected to be equal will not be equal by calling the equals method.


2 Answers

The default implementation works fine if every object is unique. But if you override equals() then you are implicitly saying that objects with different addresses can be equivalent to each other. In that case you have to also override hashCode().

Think of the String class.

String s1 = new String("foo");
String s2 = new String("foo");

These two strings are equal and so their hash codes must be equal. But they are distinct objects with different addresses.

s1 == s2         // false, different addresses
s1.equals(s2)    // true, same contents

Using their addresses as hash codes would be an error. Therefore, String overrides hashCode() to ensure that equal strings have equal hash codes. This helps meet hashCode()'s contract, which is:

If a.equals(b) is true, then a.hashCode() == b.hashCode().

Bottom line: If you override equals(), also override hashCode().

like image 119
John Kugelman Avatar answered Oct 20 '22 08:10

John Kugelman


Exact key objects used while putting objects into hashmap may not be available to access the map later on in your program. So you will end up overriding equals method. When you override equals method you make sure that their hashcodes are also equal, otherwise you cannot retrieve the objects from the hashmap.

like image 27
user640554 Avatar answered Oct 20 '22 09:10

user640554