Kotlin .get()
Published Jan 22, 2023
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
Learn Kotlin on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
- Beginner Friendly.9 hours