Java.util.Hashtable Class



Introduction

The java.util.Hashtable class implements a hashtable, which maps keys to values.Following are the important points about Hashtable −

  • In this any non-null object can be used as a key or as a value.

  • If many entries are to be made into 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.

Class declaration

Following is the declaration for java.util.Hashtable class −

public class Hashtable<K,V>
   extends Dictionary<K,V>
   implements Map<K,V>, Cloneable, Serializable

Class constructors

Sr.No. Constructor & Description
1

Hashtable()

This constructs a new, empty hashtable with a default initial capacity (11) and load factor (0.75).

2

Hashtable(int initialCapacity)

This constructs a new, empty hashtable with the specified initial capacity and default load factor (0.75).

3

Hashtable(int initialCapacity, float loadFactor)

This constructs a new, empty hashtable with the specified initial capacity and the specified load factor.

4

Hashtable(Map<? extends K,? extends V> t)

This constructs a new hashtable with the same mappings as the given Map.

Class methods

Sr.No. Method & Description
1 void clear()

This method clears this hashtable so that it contains no keys.

2 Object clone()

This method creates a shallow copy of this hashtable.

3 boolean contains(Object value)

This method tests if some key maps into the specified value in this hashtable.

4 boolean containsKey(Object key)

This method tests if the specified object is a key in this hashtable.

5 boolean containsValue(Object value)

This method returns true if this hashtable maps one or more keys to this value.

6 Enumeration<V> elements()

This method returns an enumeration of the values in this hashtable.

7 Set<Map.Entry<K,V>> entrySet()

This method returns a Set view of the mappings contained in this map.

8 boolean equals(Object o)

This method compares the specified Object with this Map for equality, as per the definition in the Map interface.

9 V get(Object key)

This method returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

10 int hashCode()

This method returns the hash code value for this Map as per the definition in the Map interface.

11 boolean isEmpty()

This method tests if this hashtable maps no keys to values.

12 Enumeration<K> keys()

This method returns an enumeration of the keys in this hashtable.

13 Set<K> keySet()

This method returns a Set view of the keys contained in this map.

14 V put(K key, V value)

This method maps the specified key to the specified value in this hashtable.

15 void putAll(Map<? extends K,? extends V> t)

This method copies all of the mappings from the specified map to this hashtable.

16 protected void rehash()

This method increases the capacity of and internally reorganizes this hashtable, in order to accommodate and access its entries more efficiently.

17 V remove(Object key)

This method removes the key (and its corresponding value) from this hashtable.

18 int size()

This method returns the number of keys in this hashtable.

19 String toString()

This method returns a string representation of this Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space).

20 Collection<V> values()

This method returns a Collection view of the values contained in this map.

Methods inherited

This class inherits methods from the following classes −

  • java.util.Object
Advertisements