FileDocCategorySizeDatePackage
LRUCacheExample.javaAPI DocExample2069Wed May 18 09:38:58 BST 2005com.discursive.jccook.collections.cache

LRUCacheExample

public class LRUCacheExample extends Object

Fields Summary
private Map
cache
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

		LRUCacheExample example = new LRUCacheExample();
		example.start();
	
private voidstart()

		cache = new LRUMap( 5 );
		
		// Populate the cache with 5 stock prices
		cache.put( "MSFT", 	new Float( 0.03 ) );
		cache.put( "TSC", 		new Float( 0.001 ) );
		cache.put( "LU", 		new Float( 23.30 ) );
		cache.put( "CSCO", 	new Float( 242.20 ) );
		cache.put( "P", 			new Float( 10.23 ) );
		
		// Now use some of the entries in the cache
		Float cscoPrice  = (Float) cache.get( "CSCO" );
		Float msPrice = (Float) cache.get( "MSFT" );
		Float tscPrice = (Float) cache.get( "TSC" );
		Float luPrice = (Float) cache.get( "LU" );
		Float pPrice = (Float) cache.get( "P" );
		Float msPrice2 = (Float) cache.get( "MSFT" );
		
		// Add another price to the Map, this should kick out the LRU item.
		cache.put( "AA",			new Float( 203.20 ) );
		
		// CSCO was the first price requested, it is therefor the
		// least recently used.
		if( !cache.containsKey("CSCO") ) {
			System.out.println( "As expected CSCO was discarded" );
		}