Java Utility Library

Java LinkedHashMap - get() Method



The java.util.LinkedHashMap.get() method returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

A return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null.

Syntax

public V get(Object key)

Here, V is the type of value maintained by the container.


Parameters

key Specify the key whose associated value is to be returned.

Return Value

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Exception

NA

Example:

In the example below, the java.util.LinkedHashMap.get() method returns the value to which the specified key is mapped in the given map.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a linkedhashmap
    LinkedHashMap<Integer, String> MyMap = new LinkedHashMap<Integer, String>();

    //populating hash map
    MyMap.put(101, "John");
    MyMap.put(102, "Marry");
    MyMap.put(103, "Kim");
    MyMap.put(104, "Jo");

    //printing mapped value of specified key
    System.out.println("101 is mapped to: " + MyMap.get(101));
    System.out.println("102 is mapped to: " + MyMap.get(102));
    System.out.println("103 is mapped to: " + MyMap.get(103));
    System.out.println("104 is mapped to: " + MyMap.get(104));  
  }
}

The output of the above code will be:

101 is mapped to: John
102 is mapped to: Marry
103 is mapped to: Kim
104 is mapped to: Jo

❮ Java.util - LinkedHashMap