Collection framework is collections of interfaces, implementations and algorithms, which are used to store, retrieve and manipulate the data. In the version of Java 1.0, HashTable was introduced where it did not use map. This version was upgraded in java 1.2, to implement Map interface making it as part of the Java Collections framework.
HashTable is an array of list used to store the information in a more synchronized way. It uses the key-value pairs to store the information within a table. To locate an element in the Hash table, first object is specified which is used as key. The key is then hashed which generates an index at which value is stored within the table.
HashTable can store only those objects which can implement both hashcode() and equals() method. When a key is hashed it generates the hash code which generates an index in the table at which value is stored. Method equals() compares the two objects. For example String implements both hashcode() and equals() method.
also read:
HashTable Features
- Hash table stores the information in a more synchronized way.
- Hash table does not allow the duplication of values. It can associate only one value to a key.
- It cannot store a null key or a null value.
- Applicable only to those objects which can implement both hashcode() and equals() method.
- Every hashing function generates a hash code which specifies an index in the table at which value is stored.
HashTable Syntax
class Hashtable<K, V>
- Where K specifies the type of key element, and V specifies the type of value element.
- We can create HashTable as shown in the following way, where key type is string and value of key is integer.
HashTable<String, Integer> ht=new HashTable<String, Integer> () ;
HashTable Constructors
Constructor | Description |
---|---|
HashTable ( ) | It is a default constructor. |
HashTable (int size) | It creates a hash table of specified size. The default size of the hash table is 11. |
HashTable (int size, float fillRatio) | It creates a hash table of specified size and specified fillRatio. This ratio must be between 0.0 and 1.0. |
HashTable (Map m) | It creates hash table that is initialized with elements in m. |
HashTable Methods
Method | Description |
---|---|
void clear ( ) | resets and removes all the key value pairs from the HashTable. |
Object clone ( ) | It creates the shallow clone of the object. i. e. clone of the object is created but not the values . |
Boolean contains (object value) | Boolean gives either true or false result. If the value of object exists in the hash table it returns true and if value of object does not exists it returns false. |
Boolean containsKey (object key) | Boolean gives either true or false result. It returns true if key matches with the existing key in the hash table, else returns false. |
Boolean containsValue (object value) | Boolean gives either true or false result. It returns true value if value is equal to value existing in the hash table and returns false if value is not found |
Enumeration elements | It enables the access of elements only one at a time in the hash table. To access the next element from hash table nextElement () method is used. |
Object get(Object key) | This method returns the object, if the value associated with the object matches the key. |
Boolean isEmpty() | If the key value pair does not exists in the hash table it returns true indicating empty hash table. |
Enumeration keys() | It creates the enumerated keys in the hash table. |
Object put(Object key, Object value) | To insert the key value pair in the hash table this method is used. It returns previous value associated with the key if key already exists in the table, else returns null value. |
Objectremove(Object key) | This method removes the key and its value from the hash table. |
int size() | It returns number of key value pairs in the hash table. |
String to String | It returns string values of hash table. |
Simple Example for HashTable
package hash_table; import java.util.Hashtable; import java.util.Enumeration; public class Update_hash { public static void main(String[] args) { Hashtable<String, String> ht = new Hashtable<String, String>(); ht.put("player 1", "sachin"); ht.put("player 2", "sehwag"); ht.put("player 3", "dhoni"); Enumeration<String> values = ht.keys(); while (values.hasMoreElements()) { String str = (String) values.nextElement(); System.out.println(str + ":" + ht.get(str)); } } }
- Hashtable ht = new Hashtable(); line creates the instance ht of the hash table which accepts the key value pair of string type.
- ht.put (“player 1”, “sachin”); inserts the key value pairs in the table. Here key=”player1” and value=” sachin”. Similarly the other two keyValue pairs are inserted into the hash table.
- Enumeration values = ht.keys(); creates the enum type for key , which enables to access only one key at a time.
- String str = (String) values.nextElement(); as the enum type is defined for the key the next element from the hash table is accessed using this method.
- System.out.println(str + “:” + ht.get(str)); statement prints the result on the output screen.
When you run the above example, you would get the following output:
Example using HashTable Methods
In the below example we have shown the use of few HashTable methods .These are size, clone, remove, clear and empty.
package hash_table; import java.util.Hashtable; public class hashtable_methods { @SuppressWarnings("unchecked") public static void main(String[] args) { Hashtable<String, String> ht = new Hashtable<String, String>(); ht.put("player 1", "sachin"); ht.put("player 2", "sehwag"); ht.put("player 3", "dhoni"); System.out.println("Size of the table: "+ ht.size()); Hashtable<String, String> htclone = new Hashtable<String, String>(); htclone=(Hashtable<String, String>)ht.clone(); System.out.println("Clone of the table: "+ ht); System.out.println("Before removing: "+ ht); ht.remove("player 2"); System.out.println("After removing: "+ ht); ht.clear(); System.out.println("Table elements after clear: "+ ht); boolean data=ht.isEmpty(); System.out.println("Is hash table empty: " + data ); } }
- ht.size ();returns the size of the table, where ht is the instance of the HashTable.
- ht.clone(); creates the clone of the table.
- ht.remove(“player 2”); removes the specified key value from the HashTable.
- ht.clear();clears the content of the HashTable.
- boolean data=ht.isEmpty();specifies whether HashTable is empty or not. In this case it returns true as Hashtable is empty.
When you run the above example, you would get the following output: