Industrial Training




Kotlin HashMap class


Kotlin HashMap is class of collection based on MutableMap interface. Kotlin HashMap class implements the MutableMap interface using Hash table. It store the data in the form of key and value pair. It is represented as HashMap or HashMap< K, V>.
The implementation of HashMap class does not make guarantees about the order of data of key, value and entries of collections.


Constructor of Kotlin HashMap class


Constructor Description
HashMap() It constructs an empty HashMap instance
HashMap(initialCapacity: Int, loadFactor: Float = 0f) It is used to constructs a HashMap of specified capacity.
HashMap(original: Map) It constructs a HashMap instance filled with contents of specified original map.

Functions of Kotlin HashMap class


Functions Description
open fun put(key: K, value: V): V? It puts the specified key and value in the map
open operator fun get(key: K): V? It returns the value of specified key, or null if no such specified key is available in map.
open fun containsKey(key: K): Boolean It returns true if map contains specifies key.
open fun containsValue(value: V): Boolean It returns true if map maps one of more keys to specified value.
open fun clear() It removes all elements from map.
open fun remove(key: K): V? It removes the specified key and its corresponding value from map

Kotlin HashMap Example 1- empty HashMap


Let's create a simple example of HashMap class define with empty HashMap of < Int, String> and add elements later. To print the value of HashMap we will either use HashMap[key] or HashMap.get(key).


fun main(args: Array){  
  
    val hashMap:HashMap = HashMap() //define empty hashmap  
    hashMap.put(1,"Ajay")  
    hashMap.put(3,"Vijay")  
    hashMap.put(4,"Praveen")  
    hashMap.put(2,"Ajay")  
    println(".....traversing hashmap.......")  
    for(key in hashMap.keys){  
        println("Element at key $key = ${hashMap[key]}")  
    }}  


Output:
.....traversing hashmap.......
Element at key 1 = Ajay
Element at key 2 = Ajay
Element at key 3 = Vijay
Element at key 4 = Praveen


Kotlin HashMap Example 2- HashMap initial capacity


HashMap can also be initialize with its initial capacity. The capacity can be changed by adding and replacing its element.


fun main(args: Array){  
  
    val hashMap:HashMap = HashMap(3)  
    hashMap.put("name","Ajay")  
    hashMap.put("city","Delhi")  
    hashMap.put("department","Software Development")  
    println(".....traversing hashmap.......")  
    for(key in hashMap.keys){  
        println("Element at key $key = ${hashMap[key]}")  
    }  
    println(".....hashMap.size.......")  
    println(hashMap.size)  
    hashMap.put("hobby","Travelling")  
    println(".....hashMap.size  after adding hobby.......")  
    println(hashMap.size)  
    println(".....traversing hashmap.......")  
    for(key in hashMap.keys){  
        println("Element at key $key = ${hashMap.get(key)}")  
    }  
}  


Output:
.....traversing hashmap.......
Element at key name = Ajay
Element at key department = Software Development
Element at key city = Delhi
.....hashMap.size.......
3
.....hashMap.size  after adding hobby.......
4
.....traversing hashmap.......
Element at key name = Ajay
Element at key department = Software Development
Element at key city = Delhi
Element at key hobby = Travelling


Kotlin HashMap Example 3- remove() and put()


The function remove() is used to replace the existing value at specified key with specified value. The put() function add a new value at specified key and replace the old value. If put() function does not found any specified key, it put a new value at specified key.


fun main(args: Array){  
  
    val hashMap:HashMap = HashMap()  
    hashMap.put(1,"Ajay")  
    hashMap.put(3,"Vijay")  
    hashMap.put(4,"Prakash")  
    hashMap.put(2,"Rohan")  
  
    println(".....traversing hashmap.......")  
    for(key in hashMap.keys){  
        println("Element at key $key = ${hashMap[key]}")  
    }  
  
    hashMap.replace(3,"Ashu")  
    hashMap.put(2,"Raj")  
    println(".....hashMap.replace(3,\"Ashu\")... hashMap.replace(2,\"Raj\").......")....")  
    for(key in hashMap.keys){  
        println("Element at key $key = ${hashMap[key]}")  
    }  
}  

Output:
.....traversing hashmap.......
Element at key 1 = Ajay
Element at key 2 = Rohan
Element at key 3 = Vijay
Element at key 4 = Prakash
.....hashMap.replace(3,"Ashu")...hashMap.put(2,"Raj")....
Element at key 1 = Ajay
Element at key 2 = Raj
Element at key 3 = Ashu
Element at key 4 = Prakash

Kotlin HashMap Example 4 - containsKey(key) and containsValue(value)


The Function containsKey() returns true if the specified key is present in HashMap or returns false if no such key exist.
The Function containsValue() is used to check whether the specified value is exist in HashMap or not. If value exists in HashMap, it will returns true else returns false.


fun main(args: Array){  
  
    val hashMap:HashMap = HashMap()  
    hashMap.put(1,"Ajay")  
    hashMap.put(3,"Vijay")  
    hashMap.put(4,"Prakash")  
    hashMap.put(2,"Rohan")  
  
    println(".....traversing hashmap.......")  
    for(key in hashMap.keys){  
        println("Element at key $key = ${hashMap[key]}")  
    }  
  
  
    println(".....hashMap.containsKey(3).......")  
    println(hashMap.containsKey(3))  
    println(".....hashMap.containsValue(\"Rohan\").......")  
    println(hashMap.containsValue("Rohan"))  
}  

Output:
.....traversing hashmap.......
Element at key 1 = Ajay
Element at key 2 = Rohan
Element at key 3 = Vijay
Element at key 4 = Prakash
.....hashMap.containsKey(3).......
true
.....hashMap.containsValue("Rohan").......
true

Kotlin HashMap Example 5 - clear()


The clear() function is used to clear all the data from the HashMap.


fun main(args: Array){  
  
    val hashMap:HashMap = HashMap()  
    hashMap.put(1,"Ajay")  
    hashMap.put(3,"Vijay")  
    hashMap.put(4,"Prakash")  
    hashMap.put(2,"Rohan")  
  
    println(".....traversing hashmap.......")  
    for(key in hashMap.keys){  
        println("Element at key $key = ${hashMap[key]}")  
    }  
  
  
    println(".....hashMap.clear().......")  
    hashMap.clear()  
    println(".....print hashMap after clear().......")  
    println(hashMap)  
}  

Output:
.....traversing hashmap.......
Element at key 1 = Ajay
Element at key 2 = Rohan
Element at key 3 = Vijay
Element at key 4 = Prakash
.....hashMap.clear().......
.....print hashMap after clear().......
{}




Hi I am Pluto.