A LinkedHashMap
is the same as HashMap
, except the LinkedHashMap
maintains the insertion order, whereas the HashMap
doesn’t. Internally the LinkedHashMap
uses the doubly-linked list to maintain the insertion order.
Read more about
LinkedHashMap
here.
The get
method of LinkedHashMap
will:
Return the Value
associated with the passed key
if there is a mapping present for the passed key
.
Return null
if there is no mapping present for the passed key
.
public V get(Object key)
The code below demonstrates how to use the get
method.
import java.util.LinkedHashMap;class GetExample {public static void main(String[] args) {// create an LinkedHashMapLinkedHashMap<Integer, String> numbers = new LinkedHashMap<>();numbers.put(1, "One");numbers.put(2, "Two");System.out.println("The LinkedHashMap is - " + numbers);System.out.println("\nGetting value for key 1 :" + numbers.get(1));System.out.println("\nGetting value for key 5 :" + numbers.get(5));}}
In the code above, we:
Create a LinkedHashMap
object with the name numbers
.
Add two entries to the numbers
.
Call the get
method for the key 1
. This will return the value associated with the key 1
. In our case one
is returned.
Call the get
method for the key 5
. This will return null
because there is no value associated for the key 5
.