null
object can be used as a key or as a value. To successfully store and retrieve objects from a hashlist, the objects used as keys must implement the hashCode
method and the equals
method.
An instance of Hashlist
has two parameters that affect its efficiency: its capacity and its load factor. The load factor should be between 0.0 and 1.0. When the number of entries in the hashlist exceeds the product of the load factor and the current capacity, the capacity is increased by calling the rehash
method. Larger load factors use memory more efficiently, at the expense of larger expected time per lookup.
If many entries are to be made into a Hashlist
, creating it with a sufficiently large capacity may allow the entries to be inserted more efficiently than letting it perform automatic rehashing as needed to grow the table.
This example creates a hashlist of numbers. It uses the names of the numbers as keys:
Hashlist numbers = new Hashlist(); numbers.put("one", new Integer(1)); numbers.put("two", new Integer(2)); numbers.put("three", new Integer(3));
To retrieve a number, use the following code:
@author Jani Lehtim�ki @version 1.41, 01/28/97 @see java.util.Hashlist#rehash()Integer n = (Integer)numbers.get("two"); if (n != null) { System.out.println("two = " + n); }
|
|
|
|
|
|
|
|
|
|