LinkedHashMap removeEldestEntry() Method in Java

The java.util.LinkedHashMap.removeEldestEntry() method in Java is used keep a track of whether the map removes any eldest entry from the map. So each time a new element is added to the LinkedHashMap, the eldest entry is removed from the map. This method is generally invoked after the addition of the elements into the map by the use of put() and putall() method.

This method allows the map to modify itself as directed by its return value. Although the method is permitted to modify the map directly, if it does so, it must return false which will be indicative of the fact that the map should not attempt any further modification leading to ambiguity. The effects of returning true after modifying the map from within this method are unspecified.

This is very useful when the map represents a cache where it allows the map to reduce memory consumption by deleting stale entries one after another.
Syntax:



private boolean removeEldestEntry(Map.Entry eldest)

Parameters: The method accepts one parameter eldest that refers to the least recently inserted entry in the map. If the map is of the access order, then eldest refers to the least recently accessed entry and will be removed if this method returns true. If the map was empty prior to the put or putAll invocation, this will be the entry that was just inserted; in other words, if the map contains a single entry, the eldest entry is also the latest and newest entry.

Return Value: The map returns true if the eldest entry is to be removed from the map and false if the entry is not to be removed or retained.

Below program is used to illustrate the working of java.util.LinkedHashMap.removeEldestEntry() method:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate removeEldestEntry()
import java.util.*;
  
public class Linked_Hash_Map_Demo {
  
    // Refers to the max size of the map following which
    // the removal takes place of the eldest entry
    private static final int MAX = 6;
  
    public static void main(String[] args)
    {
  
        // Creating the linked hashmap and implementing
        // removeEldestEntry() to MAX size
        LinkedHashMap<Integer, String> li_hash_map = 
        new LinkedHashMap<Integer, String>() {
            protected boolean removeEldestEntry(Map.Entry<Integer, String> eldest)
            {
                return size() > MAX;
            }
        };
        // Adding elements using put()
        li_hash_map.put(0, "Welcome");
        li_hash_map.put(1, "To");
        li_hash_map.put(2, "The");
        li_hash_map.put(3, "World");
        li_hash_map.put(4, "Of");
        li_hash_map.put(5, "geeks");
  
        System.out.println("" + li_hash_map);
  
        // Adding more elements
        li_hash_map.put(6, "GeeksforGeeks");
  
        // Displying the map after adding one more element
        System.out.println("" + li_hash_map);
  
        // Adding more elements
        li_hash_map.put(7, "Hello");
  
        // Displying the map after adding one more element
        System.out.println("" + li_hash_map);
    }
}

chevron_right


Output:

{0=Welcome, 1=To, 2=The, 3=World, 4=Of, 5=geeks}
{1=To, 2=The, 3=World, 4=Of, 5=geeks, 6=GeeksforGeeks}
{2=The, 3=World, 4=Of, 5=geeks, 6=GeeksforGeeks, 7=Hello}

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html#removeEldestEntry-java.util.Map.Entry-



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.