Kotlin .get()
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.
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
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.
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