Kotlin HashMap

HashMap in Kotlin is a collection that includes object pairings. The MutableMap interface is implemented in Kotlin using a Hash Table. It saves data as a key-value pair. Map keys are unique, and each key has just one value in the map. HashMap<key, value> of HashMap<K, V> are the two ways to describe it. In the article, we will discuss the HashMap features in Kotlin. HashMap is faster since it accesses the values using unique keys. It associates each value with a key, allowing us to get these values more quickly over iteration.

What are the Constructors of HashMap in Kotlin?

HashMap in Kotlin has four constructors, each with a public access modifier:

  1. HashMap(): It’s the default constructor and it creates a HashMap instance with no values.
  2. HashMap(initialCapacity: Int, loadFactor: Float = 0f) :It’s used to make a HashMap with a certain capacity. Both initialCapacity and loadFactor will be ignored if they are not utilized.
  3. HashMap(initialCapacity: Int): The HashMap with the capacity specified is constructed here. It will be ignored if the initial capacity is not utilized.
  4. HashMap(original: Map <out K, V> ): It produces a HashMap instance with the same mappings as the given map.

How to Use the HashMap in Kotlin

To understand the basics of using the HashMap in Kotlin language, we have different examples given below:

Example # 1: Program of using HashMap() basic functions in Kotlin:

We have a simple HashMap class example that starts with an empty HashMap of type Int, String and adds entries afterward. HashMap[key] or HashMap.get will be used to output the value of HashMap (key).

The variable “myHashMap” is initialized with the empty hashMap that contains a string and int type property. Then, we have printed the empty “myHashMap” value as with the message which is stored in the “printmyHashMap” function. We have used the put() function which will add the elements in the “myHashMap”. We have added four string values along with the four integers value in “myHashMap” by using the put() function. Now, the non-empty elements in the “myHashMap” will be printed by the kotlin overload print function. We have also used a for loop for the traversal of the variable “myHashMap” elements and print the elements by using “key” which will get the key of each element of variable “myHashMap”.

Then, we have a clear() function that will clear the map and make it empty. We can see the clear and empty “myHashMap” by printing it in the println function. The function “printmyHashMap” is called at the end of the code which will print the HashMap. In the “printmyHashMap” function, we have an if statement. The IsEmpty function is used in the if statement to determine whether the “myHashMap” is empty or not.

The output of the empty HashMap, added elements in the HashMap, and the elements of the key are shown on the terminal screen.

Example # 2: Program of Using a hashmap. size() and Initial Capacity Function in Kotlin:

We have set the initial capacity of a HashMap in the example. By adding and removing elements, the capacity of its elements can be modified.

In the above example code, we have the main function declaration. The HashMap is created in the main function. We have defined a variable as “val” which is assigned with the name “players”. We have specified the HashMap with the capacity “5” which is stored in the variable “players”. Now, we have used the put() function which will add content in the “player” variable. As we have mentioned the capacity, so we can only add five elements in the HashMap of the variable “player”. Then, we have a for loop function which iterates over the “player” elements and prints the elements by using the “player[key]” method which is used in the print function as an interpolation expression. Note that we have a Kotlin overload print function that will return the size of HashMap. We have added one more element in the HashMap of the “player” and by using the size() function in the Kotlin print, it will return the size after adding an element to the HashMap. In the end, we have a for loop function which will return the HashMap for each key element.

The output of HashMap of initial capacity and the output after modifying the HashMap is displayed in the image below.

Example # 3: Program of Using a hashmap.replace() Function in Kotlin:

We can replace the provided key and its matching value from the HashMap by calling replace() function. Let’s use the replace() function to know how this function works.

In the above instance, we have declared the main function. We have created a variable represented as “val” and named it as “countries”. The variable “countries” have a HashMap function of type property Int and String. Initially, the HashMap is empty. We added the elements in the HashMap by calling the put() function. We have put four “Int” elements and “String” elements in the HashMap. Through the HashMap, elements will be printed by using “countries[keys]”. After printing the HashMap elements, we have used replace() function for variable “countries” and passed an “Int” Value and “String” Value which will be replaced. Then by using the put() function, we have added the element with the replaced element. The replaced elements and the added elements will be printed by using the Kotlin overload print function. In the end, we printed the HashMap by using HashMap[key] which holds the single element of each key.

The output of the replaced element of HashMap is shown and the added element in the place of the replaced element is shown in the below screen.


We have introduced the concept of HashMap in the Kotlin with its basic introduction. We have also explained the constructors used in the HashMap and used these constructors in the different examples of HashMap. Through the use of HashMap, the performance can be better in Kotlin. The article will help you in getting the clear concept of HashMaps in Kotlin.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.