In this article we will dive into Java HashMap and it’s most commonly used methods. Java HashMap is one of the most commonly used implementation of Map interface.
- HasMap class implements the java.util.Map interface, the under lying data structure is hash table.
- Duplicate keys are not allowed but values can be duplicated.
- Insertion order is not preserved and it is based on hash code of the keys.
- Java HashMap class is non synchronized.
- The initial default capacity of Java HashMap class is 16 with a load factor of 0.75. Which means once the map reaches its capacity, the capacity will be increased by 3/4 of initial capacity.
Basic usage of HashMap in java is to store key value pairs. Each key value pair we call an Entry. Map is a collection of Entry objects.
Below image illustrates the hierarchy of Java HashMap class.
Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).
As stated at the beginning, map is a collection of entries and each entry is a key value pair. Let’s have a look into most commonly used HashMap methods that how we process map with simple examples.
In this article we saw how to use how to create HashMap in Java and its most commonly used methods with simple demonstration examples.
Still there are few important methods to discuss like how Java HashMap iterating, merging and compute , they will be discussed each one in separate article.