Java Programing laungage

java.util Projects

java.util Project 1

LRU(Least-Recently-Used) Cache Implementation

In this programming tutorials we shall be learn about the least recently used (LRU) cache. LRU Cache helps you how to use the buffer and how to limit or fixed the size of buffer and how to manage storing and retrieving process of the buffer which size is limited.

Previous Home Next

In this page of tutorials we are going to create a LRU(least-recently-used) Cache and manage it. Here the given program takes a in numeric input to fix the size of the LRU Cache. If your entry exceeds the size of the LRU Cache the the eldest element with key is removed. LRU Cache is implemented through the LinkedHashMap class. LinkedHashMap holds values with unique key. You you store values with a duplicate key then the LinkedHashMap replace the old value of that key with the new value of the key.

Description of Code

LinkedHashMap

It is a class of the java.util.*; package. This class is also used for the implementing the doubly linked list which tracks either insertion or access order.

put(Object key, Object value)

This method is used to add a value with it's unique key in the LRU Cache. In the put() method of the LinkedHashMap class pass two argument in which first is the key and another is the value.

Map.Entry

This interface returns the map view of collection . The Map.Entry objects are valid only to the duration of the iteration.

e.getKey()

This is the method of the Map.Entry class, which is return the key corresponding to the value.

e.getValue()

This is also a method of the Map.Entry class, which is used to return the value corresponding to the key of the index of the LRU Cache.

Example


package r4r;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class lrutests {
int num = 0;
LinkedHashMap<String,String> map;
public static void main (String[] args) throws IOException{
lrutests lru = new lrutests();
}
public lrutests() throws IOException{
System.out.print("Enter the size of the LRU Cache: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
num = Integer.parseInt(br.readLine());
}
catch(NumberFormatException ne){
System.out.println(ne.getMessage() + " is not a legal entry!");
System.out.println("Please enter a numeric value.");
System.exit(0);
}
map = new LinkedHashMap<String,String>() {
public boolean removeEldestEntry (Map.Entry<String,String> eldest){
return size() > num;
}
};
String ch = "N";
while(!ch.equalsIgnoreCase("Y")){
System.out.print("Enter the key: ");
String str = br.readLine();
System.out.print("Enter the value: ");
String str1 = br.readLine();
map.put (str, str1);
System.out.print("Do you want to stop the entry(Y/N)?");
ch = br.readLine();
}
for (Map.Entry<String,String> e : getAll())
System.out.println (e.getKey() + " : " + e.getValue());
}
public synchronized Collection<Map.Entry<String,String>> getAll() {
return new ArrayList<Map.Entry<String,String>>(map.entrySet());
}
}

Previous Home Next