Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

LinkedHashMap removeEldestEntry() method in Java

Last Updated on March 10, 2022 by Ria Pathak

Introduction

The linked list is one of the most important concepts and data structures to learn while preparing for interviews. Having a good grasp of Linked Lists can be a huge plus point in a coding interview.

Problem Statement

In this problem, we are given a LinkedHashMap. We will be learning about the LinkedHashMap removeEldestEntry() method.

Problem Statement Understanding

The Java method java.util.LinkedHashMap.removeEldestEntry() is used to keep track of whether the map removes any eldest entries.

  • As a result, whenever a new element is added to the LinkedHashMap, the oldest entry is removed. This method is usually called after the items have been added to the map using the put() and putall() methods.
  • This method lets the map make changes to itself based on the return value.
  • Although the method is allowed to directly edit the map, but if it does, it must return false, indicating that the map should not try any further modification as that could lead to ambiguity.
  • Returning true after altering the map from within this function has no known consequences.
  • This is especially important when the map is used as a cache, as it allows the map to save memory by eliminating stale entries one by one.

Syntax

private boolean removeEldestEntry(Map.Entry eldest)

Parameters

  • The method receives one parameter, eldest, which relates to the map’s least recently inserted entry.
  • If the map is of access order, eldest refers to the element that has been accessed the least recently, and it will be eliminated if this function returns true.
  • If the map only has one entry, the eldest entry is also the latest and newest entry.

Return Value

If the eldest entry is to be deleted from the map, the map returns true; otherwise, the map returns false.

Input

{ [ 1 , “Coding” ] , [ 2, “is” ] , [3, “fun” ] }, Max size = 3

  • Now adding a fourth entry to the map [ 4, “super” ].

Output

{ [ 2, “is” ] , [ 3, “fun” ] , [ 4, “super” ] }

Explanation

As we can see, we have added a 4th key value pair to the LinkedHashMap, the eldest entry i.e [1 , “Coding”] gets removed as the maximum size allocated to the LinkedHashMap is 3.

look at the approach by referring the best sites to learn coding.

Approach and Algorithm

  • We will create a LinkedHashMap.

     LinkedHashMap li_hash =
            new LinkedHashMap() {
                protected boolean removeEldestEntry(Map.Entry eldest)
                {
                    return size() > MAX;
                }
            };
    

  • In the above code, we are using the removeEldestEntry() method. In the method, we are checking whether the size of the map is greater than the MAX or not.

  • Now, If at any point while adding a new key-value pair, the size of the map exceeds MAX, the eldest entry from the map will get deleted.

Code Implementation


import java.util.*;
 
public class Linked_Hash_Map {

    private static final int MAX = 3;
 
    public static void main(String[] args)
    {

        LinkedHashMap li_hash =
        new LinkedHashMap() {
            protected boolean removeEldestEntry(Map.Entry eldest)
            {
                return size() > MAX;
            }
        };

        li_hash.put(1, "Coding");
        li_hash.put(2, "is");
        li_hash.put(3, "fun");

 
        System.out.println("" + li_hash);

        li_hash.put(4, "Super");

        System.out.println("" + li_hash);
    }
}

Output

{1=Coding, 2=is, 3=fun}
{2=is, 3=fun, 4=Super}

[forminator_quiz id=”4462″]
Space Complexity: O(1), as only temporary variables are being created.

So, in this article, we have tried to learn how to use the LinkedHashMap removeEldestEntry() method in Java. This is an important concept 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.

Leave a Reply

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