Javatpoint Logo
Javatpoint Logo

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

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<Int, String>(), hashMapOf<String, String>(), hashMapOf<Any, Any>() etc.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

Output:

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





Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA