Dear Learner,

Hope you are doing well

The processing costs for selecting a value from a database-table are fairly high compared to the costs having the value already in memory. So it seems preferrable to use some smart caching-mechanism that keeps often used values in your application instead of retrieving these values from resources somewhere ‘outside’.

Most frameworks have at least one cache implementation onboard, but there also exist several other implementations of caches like e.g. EHCache. Even ordinary HashMaps/Hashtables can serve as caches also.

A critial factor when using caches in Java is the size of the cache: when your cache grows too big, the Java Garbage Collector has to cleanup more often (which consumes time) or your application even crashes with a java.lang.OutOfMemoryError.

One way to control the memory-consumption of caches is to use SoftReferences in HashMaps/Hashtables, another one is to throw away old or unused content by implementing a caching-strategy like e.g. LRU.

A simple LRU-cache already ships within the components of the Java Standard Library: the LinkedHashMap. All you have to do is to tell your application whether the eldest entry in the map should be retained or removed after a new entry is inserted. Additionally a special constructor has to be used that defines the orderingMode for the map: ‘true’ for access-order (LRU), ‘false’ for insertion-order.

Suppose we want to cache a mapping of String-Names to Integer-Ids with a maximum size of 100 entries.
How that can be done is shown by the example below with the use of an Anonymous Inner Class that overrides the removeEldestEntry-method of the LinkedHashMap.

package com.edureka;

import java.util.LinkedHashMap;

import java.util.Map;

 public class LinkedHashMapExample {

      private final static int CACHE_MAX_SIZE = 100;

      private LinkedHashMap<String, Integer> cache;


      public LinkedHashMapExample() {

            this.cache = new LinkedHashMap<String, Integer>(CACHE_MAX_SIZE, 0.75f, true) {

                  protected boolean removeEldestEntry(

                             Map.Entry<String, Integer> eldest) {

                        // Remove the eldest entry if the size of the cache exceeds the

                        // maximum size

                        return size() > CACHE_MAX_SIZE;




      public Integer getIdForName(String name) {

             Integer id = cache.get(name);

             if (id != null)

                  return id;

            else {

                  id = getIdForNameFromExternal(name);

                   // TODO Tino, 24.06.2009: what to do if no id could be found for the

                  // provided name in external resource?

                  cache.put(name, id);

                  return id;






      private Integer getIdForNameFromExternal(String name) {

             // TODO Tino, 24.06.2009: replace dummy-code

            return 1;



Feel free to contact us in case you have any query. 
Kindly share your feedback by clicking on either of the smiley's.