Java – Hashtable

Java – Hashtable 2017-06-13T00:51:52+00:00

Hashtable :

Hashtable is a part of the java.util library and is a concrete implementation of a dictionary.

(Dictionary is a class that represents a key/value storage repository. Given a key and value, you can store the value in a Dictionary object. Once the value is stored, you can retrieve it by using its key.)

Hashtable stores key/value pairs in a hash table.

When using a Hashtable, you specify an object that is used as a key, and the value that you want to link to that key.

The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table.

The Hashtable constructors are shown here:

Hashtable()

Hashtable(int size)

The first constructor is the default constructor.

The second constructor creates a hash table that has an initial size specified by size.

The methods available with Hashtable are

Method Description
Void clear() Resets and empties the hash table.
Boolean containsKey(Object key) Returns true if some key equals to key exists within the hash table. Returns false if the key isn’t found.
Boolean containsValue(Object value) Returns true if some value equal to value exists within the hash table. Returns false if the value isn’t found.
Enumeration elements( ) Returns an enumeration of the values contained in the hash table.
Object get(Object key) Returns the object that contains the value associated with key. If key is not in the hash table, a null object is returned.
Boolean isEmpty( ) Returns true if the hash table is empty; Returns false if it contains at least one key.
Enumeration keys( ) Returns an enumeration of the keys contained in the hash table.
Object put(Object key Object value) Inserts a key and a value into the hash table.
Object remove(Object key) Removes key and its value. Returns the value associated with key. If key is not in the hash table, a null object is returned.
Int size( ) Returns the number of entries in the hash table.
String toString( ) Returns the string equivalent of a hash table.

EX :


 


Output :
Laxman: 88
Janki: 54
Ram: 58
String {Krishna=99, Laxman=88, Janki=54, Ram=58}
Is Hashtable empty true

Prev Next