java.lang.Object | ||
↳ | java.util.Dictionary<K, V> | |
↳ | sun.misc.Cache |
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 Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new, empty Cache with the specified initial
capacity and the specified load factor.
| |||||||||||
Constructs a new, empty Cache with the specified initial
capacity.
| |||||||||||
Constructs a new, empty Cache.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Returns an enumeration of the elements.
| |||||||||||
Gets the object associated with the specified key in the Cache.
| |||||||||||
Returns true if the Cache contains no elements.
| |||||||||||
Returns an enumeration of the Cache's keys.
| |||||||||||
Puts the specified element into the Cache, using the specified
key.
| |||||||||||
Removes the element corresponding to the key.
| |||||||||||
Returns the number of elements contained within the Cache.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Rehashes the contents of the table into a bigger table.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() | |||||||||||
![]() |
Constructs a new, empty Cache with the specified initial capacity and the specified load factor.
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. |
IllegalArgumentException | If the initial capacity is less than or equal to zero. |
---|---|
IllegalArgumentException | If the load factor is less than or equal to zero. |
Constructs a new, empty Cache with the specified initial capacity.
initialCapacity | the initial number of buckets |
---|
Constructs a new, empty Cache. A default capacity and load factor is used. Note that the Cache will automatically grow when it gets full.
Returns an enumeration of the elements. Use the Enumeration methods on the returned object to fetch the elements sequentially.
Gets the object associated with the specified key in the Cache.
key | the key in the hash table |
---|
Returns true if the Cache contains no elements.
true
if this dictionary maps no keys to values;
false
otherwise.
Returns an enumeration of the Cache's keys.
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.
key | the specified hashtable key |
---|---|
value | the specified element |
NullPointerException | If the value of the specified element is null. |
---|
Removes the element corresponding to the key. Does nothing if the key is not present.
key | the key that needs to be removed |
---|
Returns the number of elements contained within the Cache.
Rehashes the contents of the table into a bigger table. This is method is called automatically when the Cache's size exceeds the threshold.