Published Jan 22, 2023
Contribute to Docs

The get() retrieves and returns the value associated with a particular key, or null if it doesn’t exist.


myHashMap.get(key: K): V?

The key parameter is of data type K. If the key exists in myHashMap, the associated value, of type V, is returned. Otherwise, null is returned.


The following example demonstrates how the .get() function is used to retrieve a value:

fun main() {
val hashMap = HashMap<String, Int>().apply {
put("apple", 1)
put("banana", 2)
put("cherry", 3)
val value = hashMap.get("apple")

This will print the following output:


All contributors

Looking to contribute?

Learn Kotlin on Codecademy