HashMap

Introduction

In general, Map is a data structure that represent collection of key value pairs.
In Java, this is represented by Map Interface. Map comes under collection framework, but does not extend Collection interface, because Collection interface works on individual elements whereas Map works on Key Value pair. Map provide various methods like get, put, remove, entrySet to store and manage key value pair.

Map mostly used in tasks like

  • Caching
  • Representing and managing configuration settings, example Properties class in java.
  • Can use to solve problems related to graph in computer science.
  • Can use to store count/frequency of elements.

As we mentioned Map is just an interface, so it just provide specification. There are various implementation of Map, like HashMap, TreeMap.
In this article we are going to discuss about HashMap.

Content :

HashMap is the class that extends Map interface and provides implementation of it. as Map works on Key Value Pair, HashMap provides implementation to store and manage key value pairs.

There are various method we have in Map, its implementation provided by HashMap, few of them are below
V get(Object key) : to fetch value of key from HashMap
V put(K key, V value) : to store key value into HashMap and return its previous value in case no previous value was present it returns null. So it means if it returns null we can say that provided key and value storing first time.

Let's understand How HashMap store key value pairs?

Here internally HashMap uses a Bucket Array to store key value pair.

But wait, What is Bucket Array?

To understand this, lets first check Map interface, in Map interface there is nested interface named as Entry. This Map.Entry interface provide specification to manage key, value, hashcode and next. Whereas Map interface manage an array of type Map.Entry interface.
This array of type Map.Entry usually called as Bucket Array and each element in it, called Bucket.
Map and Map.Entry is just an interface, HashMap is implementation of Map and Map.Entry implementation is provided by HashMap nested class
HashMap.Entry (in <= Java version 7)
HashMap.Node (in >=Java version 8)
Here we will use Java 8 implementation further in this article.
So, important point to note is Bucket (HashMap.Node) store and manage key value pair whereas HashMap store and manage BucketArray(HashMap.Node[]).

Some useful methods provided by HashMap

  • V put(K key, V value) : put method takes two argument key and value, place it into bucket array and returns old value if key already exists , else null.
  • V get(Object key) : get method takes key as input and returns value associated with that key from bucket array.
  • boolean containsKey(Object key) : It checks if key provided in input exists in bucket array or not.
  • V remove(Object key) : It remove key value pair associated with key as provided in input from bucket array.

Some methods to iterate over HashMap

We can Iterate using Set<K> keySet() and Set<Map.Entry<K,V>> entrySet() methods on HashMap.

  • Set<K> keySet() : keySet method return set of all keys present in HashMap.
  • Set<Map.Entry<K,V>> entrySet() : entrySet method return set of all Key Value pair

Some methods introduced in HashMap as part of Java 8

  • V getOrDefault(Object key, V defaultValue) : This method returns value associated with the provided key if key exists else return the defaultValue.
  • V putIfAbsent(K key, V value) : It only put key value pairs, if key is not present in HashMap.
  • V compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) : It perform update of value associated with key, update of value happens on the basis of result of BiFunction provided as input.
  • V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction) : It put key and value if key is not present, here Value is calulate on the basis of Function provided as input
  • V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) : It perform update of value associated with key, if key is present else not do anything, update happens on the basis of result of BiFunction provided as input.
  • void forEach(BiConsumer<? super K,? super V> action) : It takes BiConcumer as input and apply it over all element of HashMap
  • V merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction) : It update the value of key, the value of key update to the value return by BiFunction provided as argument in method merge. BiFunction takes two argument, first one is existing value and second is new value (which is provided as 2nd argument in merge method).
  • boolean remove(Object key, Object value) : It remove key if combination of provided key value exists, if it removes it return true else false.
  • V replace(K key, V value) : It replace exisitng value of key by value which is provided as argument and returns old/exisiting value.
  • boolean replace(K key, V oldValue, V newValue) : It replace oldvalue associated with key k if exists by newvalue, if not exists it don't do anything.
  • void replaceAll(BiFunction<? super K,? super V,? extends V> function) : It replace values of all keys by the result of BiFunction, provided as argument of method replaceAll.

Comments