Hashmap is an implementation of the Map interface in Java. Hashmaps are based on Hash tables and it is denoted by <K, V> or <Key, Value> If you are wondering what a map in Java is. A map is a collection of key-value pairs. Hence, it maps keys to values, where a key is an object that you use to retrieve values whenever needed.
Key |
Value |
---|---|
US |
United States |
IND |
India |
GR |
Greece |
FR |
France |
For example, in the above table, it can be seen that GR is the key and its correspondent value is Greece.
There are a few points to remember about Hashmaps in Java:
Now that you know what a Hashmap is. Let’s understand how you can create a Hashmap in Java. As Hashmap is part of the java.util package, firstly, we have to import the implementation class, then declare the Hashmap class and initialize the Hashmap using the put method by inserting values into the map.
Output
Hashmap is a collection of key-value pairs and an array of nodes. It uses an array and LinkedList for storing key-value pairs. Hashmap also uses a method called Hashing that converts an object into integer form through Hashcoding. **
**
Here is the structure of the Hashmap node that is programmatically represented as a class. The Hashmap class has a similar structure to the LinkedList node and the array of these nodes is called Bucket.
The working of Hashmap is influenced by two parameters, initial capacity and LoadFactor. Initial capacity refers to the Hashmap object capacity when it is constructed and every time it’s multiplied by 2. The LoadFactor parameter measures an increase in capacity while rehashing.
Read: Design Patterns in Java
Note that when the capacity is higher than the load factor will be small as no rehashing will be needed. And in the case of lower capacity, the load factor will be high as frequent rehashing will be required. So, to create an efficient hashmap, we should carefully choose these two factors.
As we have learned to create the hashmap in Java and how hashmaps work internally, it’s time to see the methods and constructors that can be used to work with Hashmaps.
Method |
Method Prototype |
Description |
---|---|---|
clear |
void clear () |
Removes all mappings from the HashMap |
isEmpty |
boolean isEmpty () |
Checks whether the HashMap is empty. Returns true if it is. |
clone |
Object clone () |
Returns a shallow copy of the hash map without cloning the keys and values. |
entrySet |
Set entrySet () |
Returns a collection of HashMap mappings. |
keyset |
Set keySet () |
The HashMap is returned as a set of keys. |
put |
V put ( Object key, Object value) |
Adds a key-value entry to the HashMap. |
putAll |
void putAll ( Map map) |
Adds defined ‘map’ elements to the HashMap. |
putIfAbsent |
V putIfAbsent (K key, V value) |
If the key-value pair is not present it adds the given entry to the HashMap. |
remove |
V remove (Object key) |
Delete the entry for the given key from the HashMap. |
remove |
boolean remove (Object key, Object value) |
From the HashMap, deletes the specified key-value pair. |
compute |
V compute (K key, BiFunction < ? super K,? super V,? extends V > remappingFunction) |
This method computes a mapping for the given key and its current value or null value using 'remappingfunction'. |
Method |
Method Prototype |
Description |
computeIfAbsent |
V computeIfAbsent (K key, Function < ? super K,? extends V > mappingFunction) |
Computes the mapping for a key-value pair and inserts it if the value is not present or null using 'mappingFunction'. |
computeIfPresent |
V computeIfPresent (K key, BiFunction remappingFunction) |
If the key is already present and not null, the 'remappingFunction' is used to compute a new mapping. |
containsValue |
boolean containsValue ( Object value) |
Returns true if the given value exists in the HashMap. |
containsKey |
boolean containsKey (Object key) |
If there is a key matching the given key in the HashMap, this method returns true. |
equals |
boolean equals (Object o) |
This method matches a given object with the HashMap. |
forEach |
void forEach (BiConsumer < ? super K,? super V > action) |
Performs given ‘action’ for entries in the HashMap. |
get |
V get (Object key) |
It returns the given key in the object with an associated value. |
getOrDefault |
V getOrDefault (Object key, V defaultValue) |
If the given key is mapped, this method returns the value. If not, it returns the default value. |
isEmpty |
boolean isEmpty () |
Detects whether the HashMap is empty or not. |
merge |
V merge (K key, V value, BiFunction < ? super V,? super V,? extends V > remappingFunction) |
merge method determines if the given key is not associated with value or null. It then uses remappingFunction to associate it with a non-null value. |
replace |
V replace (K key, V value) |
The replace method exchanges the given value for the defined key. |
replace |
boolean replace (K key, V oldValue, V newValue) |
It substitutes the previous value of the given key with the new value. |
replaceAll |
void replaceAll (BiFunction < ? super K,? super V,? extends V > function) |
Operates the function given and replaces every value in the HashMap with the resultant function. |
values |
Collection < V > values() |
Returns the present array of values in the HashMap. |
size |
int size () |
This method gives the size of the entries in the HashMap. |
Constructors
Constructor Prototype |
Description |
---|---|
HashMap () |
Constructor by default. |
HashMap ( Map < ? extends K,? extends V > m) |
From the given map object m, create a new HashMap. |
HashMap ( int capacity) |
A new HashMap is created with the capacity specified by the argument 'capacity'. |
HashMap ( int capacity, float loadFactor ) |
The constructor creates a new HashMap with the capacity and loadFactor values. |
Conclusion
In this article, we have learned that a hashmap is a key-value pair collection that helps in finding the data just based on key and offers an efficient data view. You can easily create a hashmap by importing the Hashmap class from the java.util package. It is also an implementation of the Map interface and no input order is maintained in Hashmap.