LinkedHashMap get() Method in Java

Introduction

One of the most crucial data structures to learn while preparing for interviews is the linked list. In a coding interview, having a thorough understanding of Linked Lists might be a major benefit.

The LinkedHashMap class's java.util.LinkedHashMap.get() function is used to obtain or acquire the value mapped by a certain key specified in the argument. When there is no such mapping for the key in the map, it returns NULL.

LinkedHashMap get() Method

Syntax: LinkedHashMap.get(Object Key)

Parameters: This method accepts a single parameter, that is, the key whose value is meant to be fetched.

Return Value: The value linked with the key in the argument is returned by the method.

This method takes in a key of the type object as a parameter and returns the value associated with that key.

Input
[1 = Coding, 2 = is, 3 = Fun], Key whose value is to be printed - 3

Output
The value is Fun

Explanation: As we can see, the value associated with the specified key (3) is printed.

Code Implementation

import java.util.*;
  
public class PrepBytes{
    public static void main(String[] args)
    {
        LinkedHashMap lihashmap = 
        new LinkedHashMap();

        lihashmap.put(1, "Coding");
        lihashmap.put(2, "is");
        lihashmap.put(3, "Fun");

        System.out.println("Initial Mappings are: " + lihashmap);

        System.out.println("The Value is: " + lihashmap.get(3));
    }
}

Output

Initial Mappings are: {1=Coding, 2=is, 3=Fun}
The Value is: Fun

Time Complexity: O(1), as a hashtable, is used as its underlying data structure.

So, in this article, we have tried to explain how to use the LinkedHashMap get() method in Java. Java Collection Framework is very important when it comes to coding interviews. If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link Linked List.

Previous post LinkedList addFirst() Method in Java
Next post How to Get First or Last entry from Java LinkedHashMap

Leave a Reply

Your email address will not be published. Required fields are marked *