Kotlin hashMapOf()

In Kotlin, hashMapOf() is used to create hash maps in data structure in kotlin. HashMap is a class which stores hashmaps and to initialize its object we use hashMapOf(). hashMapOf() is a method of HashMap class and returns an instance of HashMap. It takes the key-value pair as parameter for initializatio, the parameter is optional.

Syntax:

HashMap hashMapOf(vararg pairs: Pair)  

Parameter:
It can be a null in case of creating an empty hashmap instance. Otherwise it takes key value pairs as parameter.
Return:
It returns an instance of HashMap class in kotlin depending on the parameters passed in the hashMapOf() method.
Initialization of hash map:
HashMap is initialized by writing up the key value pairs inside the hashMapOf() method as parameters.



Example:
In this example, the instantiation of HashMap is done by using hashMapOf() and parameters passed in it.

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array) {
    var hashmap:HashMap = hashMapOf(1 to "Geeks", 2 to "For", 3 to "Geeks")
    for (i in hashmap.keys) {
        println("Key = ${i}, value = ${hashmap[i]}")  
    
}

chevron_right


Output:

Key = 1, value = Geeks
Key = 2, value = For
Key = 3, value = Geeks

Important Methods:

put(key: K, value: V)

This method is used for putting the key value pair into the hash map. It is used when the hash map is not initialized i.e the value inside the hashMap() method is not passed.
Example to demonstrate the put() method –

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>) {
    var hashmap = hashMapOf<Int,String>()
    hashmap.put(1, "Geeks")
    hashmap.put(2, "For")
    hashmap.put(3, "Geeks")
    for (i in hashmap.keys) {
        println("Key = ${i}, value = ${hashmap[i]}")
    }
}

chevron_right


Output:

Key = 1, value = Geeks
Key = 2, value = For
Key = 3, value = Geeks

get(key: K)

It is used to get the value having key K of the hash map.

Example to demonstrate the get() method –

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>) {
    var hashmap = hashMapOf<Int,String>()
    hashmap.put(1, "Geeks")
    hashmap.put(2, "For")
    hashmap.put(3, "Geeks")
    for (i in hashmap.keys) {
        println("Key = ${i}, value = ${hashmap.get(i)}")
    }
}

chevron_right


Output:

Key = 1, value = Geeks
Key = 2, value = For
Key = 3, value = Geeks

remove(key: K)

It is used to remove the value from hash map having key K.

Example to demonstrate the removing of key value pair from hash map –

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>) {
    var hashmap = hashMapOf<Int,String>()
    hashmap.put(1, "Geeks")
    hashmap.put(2, "For")
    hashmap.put(3, "Geeks")
    hashmap.remove(2)
    for (i in hashmap.keys) {
        println("Key = ${i}, value = ${hashmap.get(i)}")
    }
}

chevron_right


Output:


Key = 1, value = Geeks
Key = 3, value = Geeks

containsKey(key: K)

It returns true if map contains key K.
Example: In this example, we check the existence of key 2 in the hash map and display it, if it exists.

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>) {
    var hashmap = hashMapOf<Int,String>()
    hashmap.put(1, "Geeks")
    hashmap.put(2, "For")
    hashmap.put(3, "Geeks")
    if(hashmap.containsKey(2)) {
        println(hashmap.get(2))
    }
}

chevron_right


Output:

For

containsValue(value: V)

It returns true, if map contains value V.

Example: In this example, we check existence of value “For” in the hash map and displaying it, if it exists.

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>) {
    var hashmap = hashMapOf<Int,String>()
    hashmap.put(1, "Geeks")
    hashmap.put(2, "For")
    hashmap.put(3, "Geeks")
    if(hashmap.containsValue("For")) {
        println("Value found")
    }
    else{
        println("Value not found")
    }
}

chevron_right


Output:

Value found

replace(key:k, value:v)

This function is used to replace the key k having some value with value v.

Example: In this example, we are replacing the value of key 2 with a new value.

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>) {
    var hashmap = hashMapOf<Int,String>()
    hashmap.put(1, "Geeks")
    hashmap.put(2, "For")
    hashmap.put(3, "Geeks")
    hashmap.replace(2,"to")
    for(i in hashmap.keys)
    {
        println(""+i+" "+hashmap.get(i))
    }
}

chevron_right


Output:

1 Geeks
2 to
3 Geeks

set(key, value)

It is used to set the given value at specified key if it exist. If the key does not exist in the HashMap it will add new key and set the given value corresponding to it.

Example to demonstrate the set() method –

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>) {
    var hashmap = hashMapOf<Int,String>()
    hashmap.put(1, "Geeks")
    hashmap.put(2, "For")
    hashmap.put(3, "Geeks")
    hashmap.set(4,"GFG")
    hashmap.set(2,"to")
    for(i in hashmap.keys)
    {
        println(""+i+" "+hashmap.get(i))
    }
}

chevron_right


Output:

1 Geeks
2 to
3 Geeks
4 GFG

clear()

It clears up the whole hash map i.e it removes all the elements from the hash map.

Example to demonstrate the clear() method –

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>) {
    var hashmap = hashMapOf<Int,String>()
    hashmap.put(1, "Geeks")
    hashmap.put(2, "For")
    hashmap.put(3, "Geeks")
    hashmap.clear()
    println(hashmap)
}

chevron_right


Output:

{}


My Personal Notes arrow_drop_up

Im a final year MCA student at Panjab University, Chandigarh, one of the most prestigious university of India I am skilled in various aspects related to Web Development and AI I have worked as a freelancer at upwork and thus have knowledge on various aspects related to NLP, image processing and web

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.