In Kotlin, when we want to store key-value pairs, we can use a HashMap. The function hashMapOf() is used to create an instance of HashMap. A HashMap allows us to store data in such a way that we can quickly find a value using its corresponding key.
Creating and Initializing HashMap
The hashMapOf() function is used to create and initialize a HashMap. We can pass key-value pairs while creating it, or simply create an empty HashMap and add entries later.
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 containing the key-value pairs.
Example:
fun main() {
val hashmap = hashMapOf(1 to "Geeks", 2 to "For", 3 to "Geeks")
for ((key, value) in hashmap) {
println("Key = $key, value = $value")
}
}
Output:
Key = 1, value = Geeks
Key = 2, value = For
Key = 3, value = Geeks
Important HashMap Methods in Kotlin
i. put(key, value)
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:
fun main() {
val hashmap = HashMap<Int, String>()
hashmap.put(1, "Geeks")
hashmap.put(2, "For")
hashmap.put(3, "Geeks")
for ((key, value) in hashmap) {
println("Key = $key, value = $value")
}
}
Output:
Key = 1, value = Geeks
Key = 2, value = For
Key = 3, value = Geeks
ii). get(key)
Returns the value for the given key. If the key is not present, it returns null.
Example:
fun main() {
val hashmap = hashMapOf(1 to "Geeks", 2 to "For", 3 to "Geeks")
println("Value for key 2 is: ${hashmap.get(2)}")
}
Output:
Value for key 2 is: For
iii). remove(key)
Removes the entry with the specified key.
Example:
fun main() {
val hashmap = hashMapOf(1 to "Geeks", 2 to "For", 3 to "Geeks")
hashmap.remove(2)
for ((key, value) in hashmap) {
println("Key = $key, value = $value")
}
}
Output:
Key = 1, value = Geeks
Key = 3, value = Geeks
iv). containsKey(key)
Checks whether the key exists in the HashMap. Returns true or false.
Example:
fun main() {
val hashmap = hashMapOf(1 to "Geeks", 2 to "For", 3 to "Geeks")
if (hashmap.containsKey(2)) {
println(hashmap[2])
}
}
Output:
Forv). containsValue(value)
Checks whether the value exists in the HashMap.
Example:
fun main() {
val hashmap = hashMapOf(1 to "Geeks", 2 to "For", 3 to "Geeks")
if (hashmap.containsValue("For")) {
println("Value found")
}
}
Output:
Value foundvi). replace(key, value)
Replaces the value for the given key only if the key already exists.
Example:
fun main() {
val hashmap = hashMapOf(1 to "Geeks", 2 to "For", 3 to "Geeks")
hashmap.replace(2, "to")
for ((key, value) in hashmap) {
println("$key $value")
}
}
Output:
1 Geeks
2 to
3 Geeks
vii). set(key, value) (or [] operator)
Used to add or update the value at a key. If the key is not present, it will add a new entry.
Example:
fun main() {
val hashmap = hashMapOf(1 to "Geeks", 2 to "to", 3 to "Geeks")
hashmap[4] = "GFG" // Adding a new entry
for ((key, value) in hashmap) {
println("$key $value")
}
}
Output:
1 Geeks
2 to
3 Geeks
4 GFG
viii). clear()
Removes all key-value pairs from the HashMap.
Example:
fun main() {
val hashmap = hashMapOf(1 to "Geeks", 2 to "For", 3 to "Geeks")
hashmap.clear()
println(hashmap) // Prints an empty map
}
Output:
{}