Kotlin hashMapOf()
Last Updated :
06 Dec, 2021
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 initialization, 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.
Java
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]}" )
}
}
|
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 –
Java
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]}" )
}
}
|
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 –
Java
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)}" )
}
}
|
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 –
Java
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)}" )
}
}
|
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.
Java
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 ))
}
}
|
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.
Java
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" )
}
}
|
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.
Java
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))
}
}
|
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 –
Java
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))
}
}
|
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 –
Java
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)
}
|
Output:
{}
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...