.get()

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

Syntax

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.

Example

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")
System.out.println(value)
}

This will print the following output:

1

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Kotlin on Codecademy