Hashtable
It is used to store key/value pairs in a hash table. When using a Hashtable, we can specify an object that can be used as a key and the value that you want to be linked to that key. The key is hashed and the hash code generated is used as the index where the value can be stored within the table.
We can find Hashtable in java.util package. It is the original package of util and is a concrete implementation of Dictionary. Hashtable also implements the Map Interface. Hashtable is the collections framework similar to HashMap but in a synchronized form.
Hashtable has two attributes: key and value, similar to HashMap. In Hashtable, the object is used as a key and the value we want to link to that key. The hash code acts as an index where the value is going to be store within the table.
HashTable provides some default Constructor:-
- Hashtable(): Default Constructor that shows the Hashtable Class.
- Hashtable(int s): This creates a Hashtable of size āsā.
- Hashtable(int size, float fillRatio): Creates the Hashtable of the size specified by size and fill ratio of FillRatio.
- Hashtable(Map < ? Extends K, ? extends V > t): construct the Hashtableof the given mappings.
Hashtable pre-defined methods:-
- void clear(): Reset the Hashtable.
- Object clone(): Return the duplicate of the called object from the Hashtable.
- boolean contains(Object value): Return boolean value if the object is present within the Hashtable.
- boolean containsKey(value): Return boolean value if the key is present within the Hashtable.
- boolean containsValue(Object value): Return boolean value if the object is present within the Hashtable.
- Enumeration elements(): Returns the enumeration of the values from the Hashtable.
- Object get(Object key): Returns the bool value if the object is present in the Hashtable.
- boolean isEmpty(): If the Hashtable is empty then it returns true else false.
- Enumeration keys(): Returns the enumeration of the keys.
- Object put(Object key, Object value): Insert the value at the given key; returns null if the key is not present in the Hashtable.
- void rehash(): Modification in the size of the Hashtable and rehashes all the keys of the Hashtable.
- Object remove(Object key): Remove the respective key and its value.
Syntax:
public class Hashtable extends Dictionary implements Map, Cloneable, Serializable;
Example:
Com.knowledge2life
import java.util.*;
public class Main {
public static void main(String args[]) {
Main balance = new Main();
Enumeration names;
String str;
double bal;
balance.put("Knoweledge2life", new Double(3434.34));
balance.put("Website", new Double(123.22));
names = balance.keys();
while(names.hasMoreElements()) {
str = (String) names.nextElement();
System.out.println(str + ": " + balance.get(str));
}
System.out.println();
bal = ((Double)balance.get("Website")).doubleValue();
balance.put("Knoweledge2life", new Double(bal + 1000));
System.out.println("Website new balance: " + balance.get("Website"));
}
}
OUTPUT:
Knoweledge2life: -19.08
Website: 3434.34
Website new balance: 4434.34