Class java.util.Cache
All Packages This Package Previous Next
Class java.util.Cache
java.lang.Object
|
+----java.util.Cache
-
public class
Cache
-
extends Object
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 guranteed that a subsequent get will return it.
The cache will automatically remove entries if memory is
getting tight and the entry isn't 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);
}
-
See Also:
-
hashCode,
equals,
Ref
-
Version:
-
1.20, 14 Mar 1995
-
Author:
-
Arthur van Hoff
-
Cache(int, float)
-
Construct a new, empty cache with the specified initial capacity
and the specified load factor.
-
Cache(int)
-
Constructs a new, empty cache with the specified initial capacity.
-
Cache()
-
Constructs a new, empty cache.
-
elements()
-
Returns an enumeration of the elements.
-
get(Object)
-
Gets the object associated with a key in the cache.
-
isEmpty()
-
Returns true if the cache contains no elements.
-
keys()
-
Returns an enumeration of the cache's keys.
-
put(Object, Object)
-
Puts the specified element into the cache, using the specified
key.
-
rehash()
-
Rehashes the content of the table into a bigger table.
-
remove(Object)
-
Removes the element corresponding to the key.
-
size()
-
Returns the cache's size (the number of elements it contains).
Cache
public Cache(int initialCapacity,
float loadFactor)
-
Construct 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.
Cache
public Cache(int initialCapacity)
-
Constructs a new, empty cache with the specified initial capacity.
-
Parameters:
-
initialCapacity
-
the initial number of buckets
Cache
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.
size
public int size()
-
Returns the cache's size (the number of elements it contains).
isEmpty
public boolean isEmpty()
-
Returns true if the cache contains no elements.
keys
public synchronized Enumeration keys()
-
Returns an enumeration of the cache's keys.
-
See Also:
-
elements,
Enumeration
elements
public synchronized Enumeration elements()
-
Returns an enumeration of the elements. Use the Enumeration methods on
the returned object to fetch the elements sequentially.
-
See Also:
-
keys,
Enumeration
get
public synchronized Object get(Object key)
-
Gets the object associated with a key in the cache.
-
Returns:
-
the element for the key or null if the key
is not defined in the hash table.
-
See Also:
-
put
rehash
protected void rehash()
-
Rehashes the content of the table into a bigger table.
This is method called automatically when the cache's
size exeeds a threshold.
put
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 can't be null and the element can't be null.
-
See Also:
-
get
-
Returns:
-
the old value of the key, or null if it didn't have one
remove
public synchronized Object remove(Object key)
-
Removes the element corresponding to the key. Does nothing if the
key isn't present.
-
Parameters:
-
key
-
the key that needs to be removed
-
Returns:
-
the value of key, or null if the key was not found
All Packages This Package Previous Next