Save This Page
Home » openjdk-7 » sun » misc » [javadoc | source]
public class: Cache [javadoc | source]
The Cache class. Maps keys to values. Any object can be used as a key and/or value. This is very similar to the Hashtable class, except that after putting an object into the Cache, it is not guaranteed that a subsequent get will return it. The Cache will automatically remove entries if memory is getting tight and if the entry is not referenced from outside the Cache.

To sucessfully store and retrieve objects from a hash table the object used as the key must implement the hashCode() and equals() methods.

This example creates a Cache of numbers. It uses the names of the numbers as keys:

     Cache numbers = new Cache();
     numbers.put("one", new Integer(1));
     numbers.put("two", new Integer(1));
     numbers.put("three", new Integer(1));
To retrieve a number use:
     Integer n = (Integer)numbers.get("two");
     if (n != null) {
         System.out.println("two = " + n);
 public Cache() 
 public Cache(int initialCapacity) 
 public Cache(int initialCapacity,
    float loadFactor) 
Method from sun.misc.Cache Summary:
elements,   get,   isEmpty,   keys,   put,   rehash,   remove,   size
Methods from java.util.Dictionary:
elements,   get,   isEmpty,   keys,   put,   remove,   size
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from sun.misc.Cache Detail:
 public synchronized Enumeration elements() 
    Returns an enumeration of the elements. Use the Enumeration methods on the returned object to fetch the elements sequentially.
 public synchronized Object get(Object key) 
    Gets the object associated with the specified key in the Cache.
 public boolean isEmpty() 
    Returns true if the Cache contains no elements.
 public synchronized Enumeration keys() 
    Returns an enumeration of the Cache's keys.
 public synchronized Object put(Object key,
    Object value) 
    Puts the specified element into the Cache, using the specified key. The element may be retrieved by doing a get() with the same key. The key and the element cannot be null.
 protected  void rehash() 
    Rehashes the contents of the table into a bigger table. This is method is called automatically when the Cache's size exceeds the threshold.
 public synchronized Object remove(Object key) 
    Removes the element corresponding to the key. Does nothing if the key is not present.
 public int size() 
    Returns the number of elements contained within the Cache.