In Java, obj.hashCode()
returns some value. What is the use of this hash code in programming?

- 11,360
- 15
- 54
- 77

- 3,243
- 7
- 24
- 13
-
1This question adds some more details. http://stackoverflow.com/questions/18078779/what-is-the-purpose-of-hashcode-method-in-java – MaheshVarma Sep 16 '13 at 09:38
-
3nice explanation given on this post.http://eclipsesource.com/blogs/2012/09/04/the-3-things-you-should-know-about-hashcode/ – Vijay Vankhede Jul 12 '15 at 10:32
8 Answers
hashCode()
is used for bucketing in Hash
implementations like HashMap
, HashTable
, HashSet
, etc.
The value received from hashCode()
is used as the bucket number for storing elements of the set/map. This bucket number is the address of the element inside the set/map.
When you do contains()
it will take the hash code of the element, then look for the bucket where hash code points to. If more than 1 element is found in the same bucket (multiple objects can have the same hash code), then it uses the equals()
method to evaluate if the objects are equal, and then decide if contains()
is true or false, or decide if element could be added in the set or not.
-
40Hi Buddy.. Very nice answer but i found a very interesting link for the same with easy to understand practical example : http://www.coderanch.com/t/321515/java/java/HashCode – Logicalj Aug 27 '13 at 12:11
-
Thank you Aishu. Now I got clear knowledge about the hashcode with bucket related explanation. – Balasubramani May 16 '15 at 03:02
-
2"if more than 1 element is found in the same bucket.. then it uses the `equals()` to evaluate", so what if there is only one hash code matched element found, it returns true directly? but since **multiple objects can have the same hash code** so it has to run `equals()` to evaluate if the matched element is equal, else it may give you the unexpected result, am I right? – Saorikido Mar 23 '17 at 03:46
-
Buckets aside, hashcode is a method that the object calls to determine the order to store each object in memory. If objects are equal, then their hashcode must also be equal. (converse of this statement is false) – NoName Jun 24 '17 at 06:16
-
1In what case would a programmer manually invoke the `hashCode()` method? – Cardinal System Nov 13 '17 at 01:26
-
From the Javadoc:
Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by
java.util.Hashtable
.The general contract of
hashCode
is:
Whenever it is invoked on the same object more than once during an execution of a Java application, the
hashCode
method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.If two objects are equal according to the
equals(Object)
method, then calling thehashCode
method on each of the two objects must produce the same integer result.It is not required that if two objects are unequal according to the
equals(java.lang.Object)
method, then calling thehashCode
method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java programming language.)

- 1
- 1

- 113,588
- 46
- 195
- 237
-
This answer has a lot of upvotes but it really doesn't answer the question. This answer describes what a hashcode is and the rules for it, which is great, but the question was about how the hashcode is used. – Russ Jackson Nov 14 '22 at 16:52
hashCode()
is a function that takes an object and outputs a numeric value. The hashcode for an object is always the same if the object doesn't change.
Functions like HashMap
, HashTable
, HashSet
, etc. that need to store objects will use a hashCode
modulo the size of their internal array to choose in what "memory position" (i.e. array position) to store the object.
There are some cases where collisions may occur (two objects end up with the same hashcode), and that, of course, needs to be solved carefully.

- 11,360
- 15
- 54
- 77

- 7,942
- 7
- 60
- 65
The value returned by
hashCode()
is the object's hash code, which is the object's memory address in hexadecimal.By definition, if two objects are equal, their hash code must also be equal. If you override the
equals()
method, you change the way two objects are equated and Object's implementation ofhashCode()
is no longer valid. Therefore, if you override the equals() method, you must also override thehashCode()
method as well.
This answer is from the java SE 8 official tutorial documentation
-
So the hash code is the memory address, not the message digest of the object? – the_prole Jul 17 '21 at 21:23
A hashcode is a number generated from any object.
This is what allows objects to be stored/retrieved quickly in a Hashtable.
Imagine the following simple example:
On the table in front of you. you have nine boxes, each marked with a number 1 to 9. You also have a pile of wildly different objects to store in these boxes, but once they are in there you need to be able to find them as quickly as possible.
What you need is a way of instantly deciding which box you have put each object in. It works like an index. you decide to find the cabbage so you look up which box the cabbage is in, then go straight to that box to get it.
Now imagine that you don't want to bother with the index, you want to be able to find out immediately from the object which box it lives in.
In the example, let's use a really simple way of doing this - the number of letters in the name of the object. So the cabbage goes in box 7, the pea goes in box 3, the rocket in box 6, the banjo in box 5 and so on.
What about the rhinoceros, though? It has 10 characters, so we'll change our algorithm a little and "wrap around" so that 10-letter objects go in box 1, 11 letters in box 2 and so on. That should cover any object.
Sometimes a box will have more than one object in it, but if you are looking for a rocket, it's still much quicker to compare a peanut and a rocket, than to check a whole pile of cabbages, peas, banjos, and rhinoceroses.
That's a hash code. A way of getting a number from an object so it can be stored in a Hashtable. In Java, a hash code can be any integer, and each object type is responsible for generating its own. Lookup the "hashCode" method of Object.
Source - here

- 625
- 7
- 12
Although hashcode does nothing with your business logic, we have to take care of it in most cases. Because when your object is put into a hash based container(HashSet
, HashMap
...), the container puts/gets the element's hashcode.

- 31,208
- 22
- 85
- 130
-
1No it doesn't. It puts/gets the key. The hashCode is only used for bucketing, – user207421 Apr 04 '20 at 09:53
hashCode()
is a unique code which is generated by the JVM for every object creation.
We use hashCode()
to perform some operation on hashing related algorithm like Hashtable, Hashmap etc..
The advantages of hashCode()
make searching operation easy because when we search for an object that has unique code, it helps to find out that object.
But we can't say hashCode()
is the address of an object. It is a unique code generated by JVM for every object.
That is why nowadays hashing algorithm is the most popular search algorithm.

- 425
- 6
- 21

- 35
- 1
-
7Hashcode is not unique. 2 unequal objects might return the same hashcode. – Ranielle Canlas May 28 '19 at 06:08
One of the uses of hashCode() is building a Catching mechanism. Look at this example:
class Point
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
if (x != point.x) return false;
return y == point.y;
}
@Override
public int hashCode()
{
int result = x;
result = 31 * result + y;
return result;
}
class Line
{
public Point start, end;
public Line(Point start, Point end)
{
this.start = start;
this.end = end;
}
@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Line line = (Line) o;
if (!start.equals(line.start)) return false;
return end.equals(line.end);
}
@Override
public int hashCode()
{
int result = start.hashCode();
result = 31 * result + end.hashCode();
return result;
}
}
class LineToPointAdapter implements Iterable<Point>
{
private static int count = 0;
private static Map<Integer, List<Point>> cache = new HashMap<>();
private int hash;
public LineToPointAdapter(Line line)
{
hash = line.hashCode();
if (cache.get(hash) != null) return; // we already have it
System.out.println(
String.format("%d: Generating points for line [%d,%d]-[%d,%d] (no caching)",
++count, line.start.x, line.start.y, line.end.x, line.end.y));
}

- 75
- 4
-
1What does 'building a catching mechanism' mean? And how does this code illustrate it? – user207421 Apr 04 '20 at 09:54