Kotlin Tutoial
Basics
Control Flow
Array & String
Functions
Collections
OOPs Concept
Exception Handling
Null Safety
Regex & Ranges
Java Interoperability
Miscellaneous
Android
In Kotlin, HashMap
is a part of the Kotlin Collections framework. It is a mutable collection of key-value pairs, where each key maps to exactly one value. The keys in a HashMap
are unique, and the map provides constant-time performance for basic operations like get
and put
, assuming the hash function disperses the elements properly among the buckets.
Here's a tutorial on how to use HashMap
in Kotlin:
val map1: HashMap<Int, String> = HashMap() // Empty HashMap val map2 = hashMapOf(1 to "A", 2 to "B", 3 to "C")
val animals = HashMap<String, String>() animals["Bird"] = "Parrot" animals["Mammal"] = "Lion"
Using the key, you can retrieve the corresponding value from the map:
val bird = animals["Bird"] // bird will have the value "Parrot"
val hasBird = "Bird" in animals // true val hasFish = "Fish" in animals // false val containsParrot = animals.containsValue("Parrot") // true
for ((key, value) in animals) { println("$key -> $value") }
animals.remove("Bird") // Removes the key-value pair for "Bird"
val size = animals.size // Returns the number of key-value pairs val keys = animals.keys // Returns a set of all keys val values = animals.values // Returns a collection of all values
You can use the putAll
method to add all key-value pairs from another map:
val moreAnimals = hashMapOf("Fish" to "Salmon", "Bird" to "Eagle") animals.putAll(moreAnimals)
animals.clear() // Removes all entries from the map
You can use the filter
function to get a new map that satisfies a particular condition:
val mammals = animals.filter { it.key == "Mammal" }
Retrieve a value with a default if the key doesn't exist:
val fish = animals.getOrDefault("Fish", "Unknown")
You can transform the contents of a map using the mapValues
or mapKeys
function:
val animalLengths = animals.mapValues { it.value.length }
HashMap
is a powerful collection in Kotlin that allows you to store and retrieve data in key-value pairs efficiently. It offers a variety of methods to manipulate, access, and traverse the stored data. Familiarity with HashMap
will help you manage data effectively in Kotlin.
Creating a HashMap in Kotlin:
val hashMap = HashMap<String, Int>()
HashMap initialization and declaration in Kotlin:
val ages = hashMapOf("Alice" to 25, "Bob" to 30, "Charlie" to 22)
Adding and removing elements in a Kotlin HashMap:
ages["David"] = 28 // Adding ages.remove("Charlie") // Removing
Accessing values in a HashMap in Kotlin:
val bobAge = ages["Bob"]
Iterating over a HashMap in Kotlin:
for ((name, age) in ages) { println("$name is $age years old") }
HashMap vs mutableMapOf in Kotlin:
HashMap
and mutableMapOf
.val hashMap = HashMap<String, Int>() val mutableMap = mutableMapOf<String, Int>()
Default values for HashMap in Kotlin:
val age = ages.getOrDefault("Eve", 25)
Sorting a HashMap in Kotlin:
val sortedByName = ages.toSortedMap() val sortedByAge = ages.toList().sortedBy { it.second }.toMap()
Using HashMap with data classes in Kotlin:
data class Person(val name: String, val age: Int) val people = hashMapOf( "Alice" to Person("Alice", 25), "Bob" to Person("Bob", 30) )
Handling null values in a Kotlin HashMap:
val nullableAge: Int? = ages["Unknown"]
Merging two HashMaps in Kotlin:
val additionalAges = mapOf("Eve" to 28, "Frank" to 35) ages.putAll(additionalAges)
Filtering and transforming values in a Kotlin HashMap:
filter
and map
to manipulate values.val adults = ages.filter { it.value >= 18 } val doubledAges = ages.mapValues { it.value * 2 }