Java HashCodeBefore we understand about Collection framework in java and start manipulating the objects, it is very important to understand the concept of hash code. How collection uses hash codes to store the objects in the memory location and how it is important to develop a good hash code to improve the access and manipulation of the list of objects.
To understand how collection uses hash code, we will have to understand it closely. So let’s get started.
Suppose you are working in an official government organization and your role is to manage their secret documents. They have different locker to keep documents in it. Each locker has corresponding number with it which is useful to search in order to place or retrieve documents easily.
An employee gives you a secret document and tell you to place it to the locker. Then a question immediately arrives in your mind and you asked “Which locker ?”. Then that employee tells you, see there is a 3 digit code on each document file say (ABC) and each character is given a number say (A is given number 1, B is 2 and so on up to Z 26).
So you need to decode the code to find the locker location like
ABC = A(1) + B(2) + C(3) = 6
So you need to place ABC file to the locker with the number 6. Then you reads the 3 digit codes written on the document file and then you find the locker to place the document.
And that’s the way hash code works. When you create a new object, how System decides which object should go to which location.
Simply by using hash code. Each object has a corresponding unique id number associated with it. So System takes that number and uses it’s own algorithm to calculate the location to place the object in the collection list.
Now the same employee tells you to get the file with the code “BCB”. Then you go the locker room and calculate the code for “BCB” to get the locker location. Then suddenly something comes up in your mind that the code for “BBC” and “BCB” both are same.
Yes that’s it. You need to understand this very carefully. Two different documents may have same locker room location where they are being placed. That is totally acceptable.
But that doesn’t mean you will start searching for the names from the beginning until you find the correct document in the same locker. There may be hundreds of files and documents placed in the same locker room.
The hash code only used to determine the appropriate location for a particular object. But it will not tell you how to locate the data once you find the location.
The goal of the hash code is efficiency. You can not keep all of the documents in the single locker room, it is important to distribute the files to different locations to fetch them easily.
Same thing applies in the java programming, when you try to store an element in collection. If you will store all of your data in the same memory location then it will not be easy to find particular information in efficient manner. With the help of hash code, we can distribute the object to different memory locations so it becomes easy to fetch particular information.