.get()
dakshdeepHERE27 total contributions
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
Looking to contribute?
- 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.