What is Hashmap data structure?
What is the need of Hashmap?
How Hashmap data structure API works in Java?
What is Hashcode?
Can 2 objects have same hashcode?
This is the famous interview question for the beginners as well as for experienced, So Let's see what it is all about. Hashmap is very popular data structure and found useful for solving many problems due to O(1) time complexity for both get and put operation.
What is the need of Hashmap data structure? What problem it solves?
When we have many objects and if we want to search a particular object among them,
How to search??
(Say we have 100 Student object and we want to search Student having Roll No. 35, how to do?)
We pick one object and see, this is the object we are looking for? No.
Pick next object and check again. Repeat the steps until we found the object.
Imagine the time it will take???
The time it will take to search a particular object will be even high if total objects are more.
This is where hashmap will come in picture.
Hashmap arranges the objects in such a way, that searching any object in hashmap will be done in almost O(1) complexity.How arranging items in particular way can make searching faster. Lets see with example,
Ms. Khyati is working for XYZ Computers in Bangalore and for work assignment she has to relocate to Pune for 1 month.
Khyati noted down important house items to carry for 1 month and started packing like shown below,
She packed all items randomly in 2 boxes.