Industrial Training




Kotlin HashMap: hashMapOf()


A hashMapOf() is a function of HashMap class. It returns a new HashMap with the specified contents. It contains pairs of data in the form of key and value. HashMap is mutable collection which provides both read am write functionalities.


Syntax of hashMapOf() function


inline fun  hashMapOf(): HashMap (source)  

fun  hashMapOf(vararg pairs: Pair): HashMap (source)  

Functions of Kotlin HashMap class


Function 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 hashMapOf() Example 1


The hashMapOf() function of HashMap can be declared as different generic types such as hashMapOf(), hashMapOf(), hashMapOf() etc.

fun main(args: Array< String>){  
  
    val intMap: HashMap< Int, String> = hashMapOf< Int,String>(1 to "Ashu",4 to "Rohan", 2 to "Ajeet", 3 to "Vijay")  
  
    val stringMap: HashMap< String,String> = hashMapOf< String,String>("name" to "Ashu")  
    stringMap.put("city", "Delhi")  
    stringMap.put("department", "Development")  
    stringMap.put("hobby", "Playing")  
    val anyMap: HashMap< Any, Any> = hashMapOf< Any, Any>(1 to "Ashu", "name" to "Rohsan", 2 to 200)  
    println(".....traverse intMap........")  
    for(key in intMap.keys){  
        println(intMap[key])  
    }  
    println("......traverse stringMap.......")  
    for(key in stringMap.keys){  
        println(stringMap[key])  
    }  
    println("......traverse anyMap.......")  
    for(key in anyMap.keys){  
        println(anyMap[key])  
    }  
}  


Output:
.....traverse intMap........
Ashu
Ajeet
Vijay
Rohan
......traverse stringMap.......
Ashu
Development
Delhi
Playing
......traverse anyMap.......
Rohsan
Ashu
200

Kotlin hashMapOf() Example 2 - containsKey()


The containsKey() function returns true if it contains the mention key in the HashMap, otherwise it returns false.


fun main(args: Array){  
  
    val stringMap: HashMap = hashMapOf()  
    stringMap.put("name", "Ashu")  
    stringMap.put("city", "Delhi")  
    stringMap.put("department", "Development")  
    stringMap.put("hobby", "Playing")  
  
    println("......traverse stringMap.......")  
    for(key in stringMap.keys){  
        println("Key = ${key} , value = ${stringMap[key]}")  
    }  
  
    println("......stringMap.containsKey(\"name\").......")  
    println(stringMap.containsKey("name"))  
}  

Output:
......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
......stringMap.containsKey("name").......
true


Kotlin hashMapOf() Example 3 - containsValue()


The containsValue() function returns true if it contains the mention value in the HashMap, otherwise it returns false.


fun main(args: Array){  
  
    val stringMap: HashMap = hashMapOf()  
    stringMap.put("name", "Ashu")  
    stringMap.put("city", "Delhi")  
    stringMap.put("department", "Development")  
    stringMap.put("hobby", "Playing")  
  
    println("......traverse stringMap.......")  
    for(key in stringMap.keys){  
        println("Key = ${key} , value = ${stringMap[key]}")  
    }  
  
    println(".......stringMap.containsValue(\"Delhi\")......")  
    println(stringMap.containsValue("Delhi"))  
    println(stringMap.containsValue("Mumbai"))  
}  


Output:
......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
.......stringMap.containsValue("Delhi")......
true
false


Kotlin hashMapOf() Example 4 - contains()


The contains() function returns true if it contains the mention key in the HashMap, otherwise it returns false.


fun main(args: Array){  
  
    val stringMap: HashMap = hashMapOf()  
    stringMap.put("name", "Ashu")  
    stringMap.put("city", "Delhi")  
    stringMap.put("department", "Development")  
    stringMap.put("hobby", "Playing")  
  
    println("......traverse stringMap.......")  
    for(key in stringMap.keys){  
        println("Key = ${key} , value = ${stringMap[key]}")  
    }  
  
    println("......stringMap.contains(\"city\").......")  
    println(stringMap.contains("city"))  
}  

Output:
......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
......stringMap.contains("city").......
true


Kotlin hashMapOf() Example 5 - replace(key, value)


The replace(key, value) function is used to replace the existing value at specified key with new specified value. The replace(key, value) function returns the replaced value.


fun main(args: Array){  
  
    val stringMap: HashMap = hashMapOf()  
    stringMap.put("name", "Ashu")  
    stringMap.put("city", "Delhi")  
    stringMap.put("department", "Development")  
    stringMap.put("hobby", "Playing")  
  
    println("......traverse stringMap.......")  
    for(key in stringMap.keys){  
        println("Key = ${key} , value = ${stringMap[key]}")  
    }  
  
    println("......stringMap.replace(\"city\",\"Mumbai\").......")  
    println(stringMap.replace("city","Mumbai"))  
    println("......traverse stringMap after stringMap.replace(\"city\",\"Mumbai\").......")  
    for(key in stringMap.keys){  
        println("Key = ${key} , value = ${stringMap[key]}")  
    }  
}  


Output:
......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
......stringMap.replace("city","Mumbai").......
Delhi
......traverse stringMap after stringMap.replace("city","Mumbai").......
Key = city , value = Mumbai
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing


Kotlin hashMapOf() Example 6 - replace(key, oldValue, newValue)


The replace(key, oldValue, newValue) function is used to replace the existing old value at specified key with new specified value. The replace(key, newValue, oldValue) function returns true if it replace old value with new else it returns false.


fun main(args: Array){  
  
    val stringMap: HashMap = hashMapOf()  
    stringMap.put("name", "Ashu")  
    stringMap.put("city", "Delhi")  
    stringMap.put("department", "Development")  
    stringMap.put("hobby", "Playing")  
  
    println("......traverse stringMap.......")  
    for(key in stringMap.keys){  
        println("Key = ${key} , value = ${stringMap[key]}")  
    }  
  
    println(".......stringMap.replace(\"department\", \"Development\",\"Management\")......")  
    println(stringMap.replace("department", "Development","Management"))  
    println("......traverse stringMap after stringMap.replace(\"department\", \"Development\",\"Management\").......")  
    for(key in stringMap.keys){  
        println("Key = ${key} , value = ${stringMap[key]}")  
    }  
}  


Output:
......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
.......stringMap.replace("department", "Development","Management")......
true
......traverse stringMap after stringMap.replace("department", "Development","Management").......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Management
Key = hobby , value = Playing


Kotlin hashMapOf() Example 7 - hashMapOf().size, hashMapOf().key


The size property of hashMapOf() function returns total size of HashMap and the key property returns all keys of HashMap.


fun main(args: Array){  
  
    val stringMap: HashMap = hashMapOf()  
    stringMap.put("name", "Ashu")  
    stringMap.put("city", "Delhi")  
    stringMap.put("department", "Development")  
    stringMap.put("hobby", "Playing")  
  
    println("......traverse stringMap.......")  
    for(key in stringMap.keys){  
        println("Key = ${key} , value = ${stringMap[key]}")  
    }  
  
    println(".....stringMap.size........")  
    println(stringMap.size)  
  
    println(".......stringMap.keys......")  
    println(stringMap.keys)  
}  


Output:
......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
.....stringMap.size........
4
.......stringMap.keys......
[city, name, department, hobby]

Kotlin hashMapOf() Example 8 - getValue(key), getOrDefault(key, defaultValue)


The getValue() function returns value of specified key of the HashMap. Whereas getOrDefault() function returns corresponding value of specified key if it exist in the HashMap or it returns mentioned default value if no such key exists in HashMap.


fun main(args: Array){  
  
    val stringMap: HashMap = hashMapOf()  
    stringMap.put("name", "Ashu")  
    stringMap.put("city", "Delhi")  
    stringMap.put("department", "Development")  
    stringMap.put("hobby", "Playing")  
  
    println("......traverse stringMap.......")  
    for(key in stringMap.keys){  
        println("Key = ${key} , value = ${stringMap[key]}")  
    }  
  
    println(".......stringMap.getValue(\"department\")......")  
    println(stringMap.getValue("department"))  
  
    println(".......stringMap.getOrDefault(\"name\", \"Default Value\")......")  
    println(stringMap.getOrDefault("name", "Default Value"))  
}  


Output:
......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
.......stringMap.getValue("department")......
Development
.......stringMap.getOrDefault("name", "Default Value")......
Ashu


Kotlin hashMapOf() Example 9 - remove(key)


The remove(key) function is used to remove the specified key along with its corresponding value. The remove(key) function returns the removed value.


fun main(args: Array){  
  
    val stringMap: HashMap = hashMapOf()  
    stringMap.put("name", "Ashu")  
    stringMap.put("city", "Delhi")  
    stringMap.put("department", "Development")  
    stringMap.put("hobby", "Playing")  
  
    println("......traverse stringMap.......")  
    for(key in stringMap.keys){  
        println("Key = ${key} , value = ${stringMap[key]}")  
    }  
  
        println("......stringMap.remove(\"city\").......")  
        println(stringMap.remove("city"))  
        println("......traverse stringMap after stringMap.remove(\"city\").......")  
        for(key in stringMap.keys){  
        println("Key = ${key} , value = ${stringMap[key]}")  
    }  
}  


Output:
......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
......stringMap.remove("city").......
Delhi
......traverse stringMap after stringMap.remove("city").......
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing


Kotlin hashMapOf() Example 10 - remove(key, value)


The remove(key, value) function is used to remove the specified key along with its corresponding value. The remove(key, value) function returns true if it remove the specified key along with its value else it returns false.


fun main(args: Array){  
  
    val stringMap: HashMap = hashMapOf()  
    stringMap.put("name", "Ashu")  
    stringMap.put("city", "Delhi")  
    stringMap.put("department", "Development")  
    stringMap.put("hobby", "Playing")  
  
    println("......traverse stringMap.......")  
    for(key in stringMap.keys){  
        println("Key = ${key} , value = ${stringMap[key]}")  
    }  
        println(".......stringMap.remove(\"hobby\",\"Playing\")......")  
        println(stringMap.remove("hobby","Playing"))  
    println("......traverse stringMap after stringMap.remove(\"hobby\",\"Playing\").......")  
    for(key in stringMap.keys){  
        println("Key = ${key} , value = ${stringMap[key]}")  
    }  
}  

Output:
......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
.......stringMap.remove("hobby","Playing")......
true
......traverse stringMap after stringMap.remove("hobby","Playing").......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development

Kotlin hashMapOf() Example 11 - set(key, value)


The set(key, value) function 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.


fun main(args: Array){  
  
    val stringMap: HashMap = hashMapOf()  
    stringMap.put("name", "Ashu")  
    stringMap.put("city", "Delhi")  
    stringMap.put("department", "Development")  
    stringMap.put("hobby", "Playing")  
  
    println("......traverse stringMap.......")  
    for(key in stringMap.keys){  
        println("Key = ${key} , value = ${stringMap[key]}")  
    }  
  
    stringMap.set("name","Ashutosh")  
    stringMap.set("skill","Kotlin")  
    println("......traverse stringMap after stringMap.set(\"name\",\"Ashutosh\") and   stringMap.set(\"skill\",\"Kotlin\").......")  
    for(key in stringMap.keys){  
        println("Key = ${key} , value = ${stringMap[key]}")  
    }  
}  

Output:
......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
......stringMap.set("name","Ashutosh").......
......traverse stringMap after stringMap.set("name","Ashutosh") and stringMap.set("skill","Kotlin").......
Key = city , value = Delhi
Key = skill , value = Kotlin
Key = name , value = Ashutosh
Key = department , value = Development
Key = hobby , value = Playing

Kotlin hashMapOf() Example 12 - clear()


The clear() function is used to clear (or remove) all the key, value pair from the HashMap


fun main(args: Array){  
  
    val stringMap: HashMap = hashMapOf()  
    stringMap.put("name", "Ashu")  
    stringMap.put("city", "Delhi")  
    stringMap.put("department", "Development")  
    stringMap.put("hobby", "Playing")  
  
    println("......traverse stringMap.......")  
    for(key in stringMap.keys){  
        println("Key = ${key} , value = ${stringMap[key]}")  
    }  
  
        println("......stringMap.clear().......")  
        println(stringMap.clear())  
        println(stringMap)  
  
}  

Output:
......traverse stringMap.......
Key = city , value = Delhi
Key = name , value = Ashu
Key = department , value = Development
Key = hobby , value = Playing
......stringMap.clear().......
kotlin.Unit
{}




Hi I am Pluto.