HashMaps
HashMaps are unordered collections of key-value pairs that are implemented using a hash table. They offer efficient storage and data retrieval because of how keys are mapped to indices in an array. In Kotlin, a HashMap is represented by the HashMap
class.
Syntax
val map = HashMap<KeyType, ValueType>()
Here, KeyType
is the type of the keys in the map, and ValueType
is the type of the values.
The types can also be specified when creating an instance of the HashMap
class like this:
val map: HashMap<KeyType, ValueType> = HashMap()
The mutableMapOf()
function is used to create a mutable HashMap:
A mutable or immutable HashMap
is created with the mutableMapOf()
or mapOf()
methods, respectively:
val mutableMap = mutableMapOf<KeyType, ValueType>()
val immutableMap = mapOf<KeyType, ValueType>()
Accessing Items
To access an item in a HashMap
, the indexing operator []
is used with the key of the desired value. If the key exists in the hashmap, the indexing operator will return the corresponding value. If the key does not exist in the hashmap, the indexing operator will return null.
fun main() {val locationsMap = HashMap<String, String>()locationsMap.put("USA", "Washington D.C.")locationsMap.put("India", "New Delhi")locationsMap.put("Nigeria", "Abuja")locationsMap.put("France", "Paris")val capital = locationsMap["USA"]println(capital)}
This will print the following output:
Washington D.C.
You can also use the .get(key)
method to access the value associated with a specific key. This method returns the value associated with the key if it exists in the hashmap, or null if it does not.
val capital = locationsMap.get("India")println(capital)
This will print the following output:
New Delhi
Adding Items
To add an item to a hashmap, you can use the .put(key, value)
method. This method adds a new key-value pair to the hashmap. If the key already exists in the hashmap, the value associated with the key is updated.
locationsMap.put("China", "Beijing")
The indexing operator []
can also be used to add a new key-value pair to the hashmap. This method is useful to add a new item to the hashmap and it is not known if the key already exists.
locationsMap["Germany"] = "Berlin"
The following prints the capital cities of the countries:
fun main() {val locationsMap = HashMap<String, String>()locationsMap.put("USA", "Washington D.C.")locationsMap.put("India", "New Delhi")locationsMap.put("Nigeria", "Abuja")locationsMap.put("France", "Paris")locationsMap.put("China", "Beijing")locationsMap["Germany"] = "Berlin"for((country, capital) in locationsMap) {println("$capital, $country")}}
The output will be:
Washington D.C., USABeijing, ChinaAbuja, NigeriaParis, FranceBerlin, GermanyNew Delhi, India
HashMaps
- .get()
- Retrieves and returns the value associated with a particular key in a HashMap.
- .put()
- Adds a key-value pair to a hash map.
- .remove()
- Used to remove a key-value pair from a HashMap.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.