public class

Cache

extends Dictionary<K, V>
java.lang.Object
   ↳ java.util.Dictionary<K, V>
     ↳ sun.misc.Cache

Class Overview

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);
      }
 

Summary

Public Constructors
Cache(int initialCapacity, float loadFactor)
Constructs a new, empty Cache with the specified initial capacity and the specified load factor.
Cache(int initialCapacity)
Constructs a new, empty Cache with the specified initial capacity.
Cache()
Constructs a new, empty Cache.
Public Methods
synchronized Enumeration elements()
Returns an enumeration of the elements.
synchronized Object get(Object key)
Gets the object associated with the specified key in the Cache.
boolean isEmpty()
Returns true if the Cache contains no elements.
synchronized Enumeration keys()
Returns an enumeration of the Cache's keys.
synchronized Object put(Object key, Object value)
Puts the specified element into the Cache, using the specified key.
synchronized Object remove(Object key)
Removes the element corresponding to the key.
int size()
Returns the number of elements contained within the Cache.
Protected Methods
void rehash()
Rehashes the contents of the table into a bigger table.
[Expand]
Inherited Methods
From class java.util.Dictionary
From class java.lang.Object

Public Constructors

public Cache (int initialCapacity, float loadFactor)

Constructs a new, empty Cache with the specified initial capacity and the specified load factor.

Parameters
initialCapacity the initial number of buckets
loadFactor a number between 0.0 and 1.0, it defines the threshold for rehashing the Cache into a bigger one.
Throws
IllegalArgumentException If the initial capacity is less than or equal to zero.
IllegalArgumentException If the load factor is less than or equal to zero.

public Cache (int initialCapacity)

Constructs a new, empty Cache with the specified initial capacity.

Parameters
initialCapacity the initial number of buckets

public Cache ()

Constructs a new, empty Cache. A default capacity and load factor is used. Note that the Cache will automatically grow when it gets full.

Public Methods

public synchronized Enumeration elements ()

Returns an enumeration of the elements. Use the Enumeration methods on the returned object to fetch the elements sequentially.

Returns
  • an enumeration of the values in this dictionary.

public synchronized Object get (Object key)

Gets the object associated with the specified key in the Cache.

Parameters
key the key in the hash table
Returns
  • the value to which the key is mapped in this dictionary;
See Also

public boolean isEmpty ()

Returns true if the Cache contains no elements.

Returns
  • true if this dictionary maps no keys to values; false otherwise.

public synchronized Enumeration keys ()

Returns an enumeration of the Cache's keys.

Returns
  • an enumeration of the keys in this dictionary.

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.

Parameters
key the specified hashtable key
value the specified element
Returns
  • the old value of the key, or null if it did not have one.
Throws
NullPointerException If the value of the specified element is null.
See Also

public synchronized Object remove (Object key)

Removes the element corresponding to the key. Does nothing if the key is not present.

Parameters
key the key that needs to be removed
Returns
  • the value of key, or null if the key was not found.

public int size ()

Returns the number of elements contained within the Cache.

Returns
  • the number of keys in this dictionary.

Protected Methods

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.