Hashmap is one of the most important data structure when it comes to accessing the data in the constant amount of time. Hashmap works in almost O(1) amount of time, which means it takes a constant amount of time to access the data. Now, many of you must be wondering what the heck he is talking about constant time and notation like “O(1)”. Don’t worry I am going to explain everything in detail.
HashMap Real Life Explanation :
Many of you have read a book, have you ever struggled with finding a page which you were reading once you close a book. Yes ! what a solution to this problem? How can you go to the page you were reading directly? Many of you already have an answer in your mind, yes bookmark is the object that can help you find a page in a book easily. Great! you are one step closer to understanding how hashmap works.
Let’s try to analyse the real-life example and come up with the analogy between logical hashmap and the real-life situation. HashMap consists of the Key and Value pair. Key - unique value and Value - can be anything (String, integer, a book, a Page, a Person).
Key relates to the bookmark that helps us find a page in constant time.
Value relates to the page which we were reading.
Now here the term constant time comes into the picture. Always think of in terms of large data set and try to come up with the better solution. Let’s say you have a book equal to your height with many pages and that book won’t fit in your hand. And you want to search the page, you were reading. What do you think how much time you will take? You start, you randomly pick one page, you realise no it’s not the page you want to read, you try again and again and again. It takes some time to find the page you were reading. This “search time” goes on increasing if the book with pages goes on increasing. This is what is termed as time complexity in the algorithm. Time to do the particular operation.
Now try using a bookmark, what do you think how much time will be required to search a particular page, even if the book has “n” number of pages. Yes ! you got it, it will take constant time. No matter how big book you are reading you will find a page in “fraction of time” but that time is so negligible which is almost termed as “constant” in computer science.
Demonstration of time difference :
Run the below program, you will find the difference yourself.
Program with HashMap :
Program without HashMap :
How HashMap is implemented in Java:
HashMap takes an extra space which grow with number of input. So the space complexity of the hashmap tends to O(n). where n is the number of input.
Let look at how the HashMap can be implemented: We will look at the creation of the hashmap from our own perspective.
The above chunk represents the user defined data structure for Map. It’s more of the generic structure, containing the Key and the Value. The Key and the value can be any object.
This code is generated Hash based on the key and brings it within the range of the array capacity. It’s like you have n number of buckets and you put an object into the buckets each hash generated point you to the bucket. Hashcode function usually refers memory address.
The put and the get method are implemented as shown above. The hashmap above is implemented using ArrayList of LinkedList. Give it a try. If any question or suggestion please comment below.
If you like the blog please follow me on Github, Linkedin, twitter at the links mentioned above.