Hashcode is used to allow objects to be stored in a Map
object (or with other data structures that use hashes). The goal of a hashcode is to provide a unique value for objects with different values, but produce the same hashcode if the object is the same. This is essentially a unique index for an object to be looked up by.
A HashMap
, which implements the Map
interface, depends on a good implementation of hashcode()
to evenly distribute among differing objects for optimal performance. With this it can provide O(1) average case speed to operations such as get()
.
So if you ever intend on using a custom object that you make as a key
for a HashMap
you should provide an implementation of hashcode()
which most IDE's will assist you with.
edit: in your example output of -5468287
that is its hashcode value. If you look at the hashcode of "first strings"
which only differs by one character it should be a vastly different number, which is a good thing because it helps evenly distribute objects inside your Map.