Search Tutorials


Overriding hashcode method in Java - Hello World Example | JavaInUse



Overriding hashcode() in Java



Overview

Apart from the primitives, everything in Java is an object which is extended by the Java.lang.Object. Here is the Object class methods.
Method Description
boolean equals (Object obj) Decides whether two objects are meaningfully equivalent.
void finalize() Called by garbage collector when the garbage collector sees that the object cannot be referenced.
final void notify() Wakes up a thread that is waiting for this object's lock.
final void notifyAll() Wakes up all threads that are waiting for this object's lock.
final void wait() Causes the current thread to wait until another thread calls notify() or notifyAll() on this object.
hashCode() Returns a hashcode int value for an object, so that the object can be used in Collection classes that use hashing, including Hashtable, HashMap, and HashSet.
toString() If a subclass does not override this method, it returns a "text representation" of the object.
Here hashcode()and other methods are defined in the Object class. So all java classes have the hashcode() method by default. We can override these methods in our classes.

Lets Begin

As explained before all classes extend the object class. So Employee class implicitly extends the object class as follows-

java5-5
Hashcode() is a method to return an unique integer which is used for indentifying the bucket where this object will be stored for hashing based collections like HashMap. If not overriden, bydefault it returns the integer representing its memory address where it is stored.
Hashing is a technique used to store objects for faster access to records given a key. The key is a set of fields values of which uniquely identify a record in the file. A hash function maps key values to a number that indicates bucket where the object having the key value is stored. Consider the following class Employee
package com.javainuse.domain;

public class Employee {

	private String name;

	public Employee(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public int hashCode() {
		return name.length()-1;
	}

}

Here we have overriden the hashcode() method. Hashcode returns bucket number for the object based on number of letters in employee name after subtracting 1 from it.
java1_chap2_1
The hascode implementation may differ from the one implemented here.
Record with key value 'Kabir' maps to bucket 1 and hence the record is stored in bucket 1. Record with key value 'Tom' maps to bucket 2 and hence the record is stored in bucket 2.
The hashcode maps key Jim to the same bucket as Tom. This is called collision.

See Also

Internal working of ConcurrentHashMap in Java Image Comparison in Java Java - PermGen space vs Heap space Java - PermGen space vs MetaSpace Implement Counting Sort using Java + Performance Analysis Java 8 Features Java Miscelleneous Topics Java Basic Topics Java- Main Menu