CHAPTER 21: The Package java.util Previous
Previous
Java Language
Java Language
Index
Index
Next
Next

21.5 The Class java.util.Hashtable

21.5.1 Hashtable , 21.5.2 Hashtable , 21.5.3 Hashtable , 21.5.4 toString , 21.5.5 clone , 21.5.6 size , 21.5.7 isEmpty , 21.5.8 get , 21.5.9 put , 21.5.10 remove , 21.5.11 keys , 21.5.12 elements , 21.5.13 contains , 21.5.14 containsKey , 21.5.15 rehash , 21.5.16 clear

The class Hashtable implements the abstract class Dictionary (S21.4), with some additional functionality.

public class Hashtable extends Dictionary implements Cloneable {
	public Hashtable(int initialCapacity, float loadFactor);
	public Hashtable(int initialCapacity);
	public Hashtable();
	public String toString();
	public Object clone();
	public int size();
	public boolean isEmpty();
	public Object get(Object key)
		throws NullPointerException;
	public Object put(Object key, Object value)
		throws NullPointerException;
	public Object remove(Object key)
		throws NullPointerException;
	public Enumeration keys();
	public Enumeration elements();
	public boolean contains(Object value);
	public boolean containsKey(Object key);
	protected void rehash();
	public void clear();
}

A Hashtable 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 hashtable exceeds the product of the load factor and the current capacity, the capacity is increased, using 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 in a Hashtable, 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.


21.5.1 Hashtable

public Hashtable(int initialCapacity, float loadFactor)

This constructor initializes a newly created Hashtable object so that its capacity is initialCapacity and its load factor is loadFactor. Initially, there are no entries in the table.


21.5.2 Hashtable

public Hashtable(int initialCapacity)

This constructor initializes a newly created Hashtable object so that its capacity is initialCapacity and its load factor is 0.75. Initially, there are no entries in the table.


21.5.3 Hashtable

public Hashtable()

This constructor initializes a newly created Hashtable object so that its load factor is 0.75. Initially, there are no entries in the table.


21.5.4 toString

public String toString()

This Hashtable is represented in string form as a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space). Each entry is rendered as the key, an equals sign =, and the associated element, where the toString method is used to convert the key and element to strings.

Overrides the toString method of Object (S21.2.3).


21.5.5 clone

public Object clone()

A copy of this Hashtable is constructed and returned. All the structure of the hashtable itself is copied, but the keys and elements are not cloned.

Overrides the clone method of Object (S21.2.6).


21.5.6 size

public int size()

Implements the size method of Dictionary (S21.4.1).


21.5.7 isEmpty

public boolean isEmpty()

Implements the isEmpty method of Dictionary (S21.4.2).


21.5.8 get

public Object get(Object key)

Implements the get method of Dictionary (S21.4.3).


21.5.9 put

public Object put(Object key, Object value)

Implements the put method of Dictionary (S21.4.4).


21.5.10 remove

public Object remove(Object key)

Implements the remove method of Dictionary (S21.4.5).


21.5.11 keys

public Enumeration keys()

Implements the keys method of Dictionary (S21.4.6).


21.5.12 elements

public Enumeration elements()

Implements the elements method of Dictionary (S21.4.7).


21.5.13 contains

public boolean contains(Object value)

The result is true if and only if this Hashtable contains at least one entry for which the element is equal to value, as determined by the equals method (S20.1.3).


21.5.14 containsKey

public boolean containsKey(Object key)

The result is true if and only if this Hashtable contains an entry for which the key is equal to key, as determined by the equals method (S20.1.3). In other words, this method produces the same result as the expression:

get(key) != null


21.5.15 rehash

protected void rehash()

This Hashtable is increased in capacity and reorganized internally, in order to accommodate and access its entries more efficiently.


21.5.16 clear

public void clear()

The clear method removes all entries from this Hashtable.

Top© 1996 Sun Microsystems, Inc. All rights reserved.