This class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value.
To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.
- It is similar to HashMap, but is synchronised.
- Hashtable stores key/value pair in hash table.
- In Hashtable we specify an object that is used as a key, and the value we want to associate 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.
Constructors:
- Hashtable(): This is the default constructor.
- Hashtable(int size): This creates a hash table that has initial size specified by size.
- Hashtable(int size, float fillRatio): This version creates a hash table that has initial size specified by size and fill ratio specified by fillRatio. fill ratio: Basically it determines how full hash table can be before it is resized upward.and its Value lie between 0.0 to 1.0
- Hashtable(Map m): This creates a hash table that is initialised with the elements in m.
Methods:
- void clear() : method clears the hashtable so that it contains no keys.
Syntax : public void clear() Returns : NA Exception : NA - Object clone() : used to create a shallow copy of this hashtable.
Syntax : public Object clone() Returns :method call returns a clone of the hashtable. Exception : NA// Java code illustrating clear() and clone() methodsimportjava.util.*;classhashTabledemo {publicstaticvoidmain(String[] arg){// creating a hash tableHashtable h =newHashtable();Hashtable h1 =newHashtable();h.put(3,"Geeks");h.put(2,"forGeeks");h.put(1,"isBest");// create a clone or shallow copy of hash table hh1 = (Hashtable)h.clone();// checking clone h1System.out.println("values in clone: "+ h1);// clear hash table hh.clear();// checking hash table hSystem.out.println("after clearing: "+ h);}}chevron_rightfilter_noneOutput:
values in clone: {3=Geeks, 2=forGeeks, 1=isBest} after clearing: {} - computeIfAbsent(Key, Function): The computeIfAbsent(Key, Function) method of Hashtable class which allows you to compute value of a mapping for specified key if key is not already associated with a value (or is mapped to null).
Syntax: public <K, V> computeIfAbsent(K key, Function<K, V> remappingFunction) Parameters: This method accepts two parameters:- key: key with which the value is to be associated.
- remappingFunction: function to do the operation on value.
Below programs illustrate the computeIfAbsent(Key, Function) method:
// Java program to demonstrate// computeIfAbsent(Key, Function) method.importjava.util.*;publicclassGFG {// Main methodpublicstaticvoidmain(String[] args){// create a table and add some valuesMap<String, Integer> table =newHashtable<>();table.put("Pen",10);table.put("Book",500);table.put("Clothes",400);table.put("Mobile",5000);// print map detailsSystem.out.println("hashTable: "+ table.toString());// provide value for new key which is absent// using computeIfAbsent methodtable.computeIfAbsent("newPen", k ->600);table.computeIfAbsent("newBook", k ->800);// print new mappingSystem.out.println("new hashTable: "+ table);}}chevron_rightfilter_noneOutput:
hashTable: {Book=500, Mobile=5000, Pen=10, Clothes=400} new hashTable: {newPen=600, Book=500, newBook=800, Mobile=5000, Pen=10, Clothes=400} - contains(Object value): The java.util.Hashtable.contains(Object value) method in Java is used to check whether a particular value is being mapped by any keys present in the Hashtable.
Syntax: Hash_table.contains(Object value) Parameters: The method accepts one parameter value of object type and refers to the value of the hashtable whose mapping is to be verified. Return Value: The method returns a boolean true value if the passed value is mapped by any of the keys in the Hashtable. Exceptions: The method throws NullPointerException if the passed value is Null.Below programs illustrate the working of the above-mentioned method:
// Java code to illustrate the contains() methodimportjava.util.*;publicclassHash_Table_Demo {publicstaticvoidmain(String[] args){// Creating an empty HashtableHashtable<Integer, String> hash_table =newHashtable<Integer, String>();// Mapping string values to int keyshash_table.put(10,"Geeks");hash_table.put(15,"4");hash_table.put(20,"Geeks");hash_table.put(25,"Welcomes");hash_table.put(30,"You");// Displaying the HashMapSystem.out.println("Initial Table is: "+ hash_table);// Checking for the Value 'Geeks'System.out.println("Is the value 'Geeks' present? "+ hash_table.contains("Geeks"));// Checking for the Value 'World'System.out.println("Is the value 'World' present? "+ hash_table.contains("World"));}}chevron_rightfilter_noneOutput:
Initial Table is: {10=Geeks, 20=Geeks, 30=You, 15=4, 25=Welcomes} Is the value 'Geeks' present? true Is the value 'World' present? false - boolean containsKey(Object key) : tests if some key maps into the specified value in this hashtable.
Syntax :public boolean containsKey(Object key) Returns :returns true if and only if the specified object is a key in this hash table. Exception :NullPointerException is thrown if the key is null.
// Java code illustrating containsKey() methodimportjava.util.*;classhashTabledemo {publicstaticvoidmain(String[] arg){// creating a hash tableHashtable marks =newHashtable();// enter name/marks pairmarks.put("tweener",newInteger(345));marks.put("krantz",newDouble(245.78));marks.put("burrows",newInteger(790));marks.put("tancredi",newDouble(365.98));marks.put("bellick",newInteger(435));// check whether a value exists or notif(marks.contains(345))System.out.println("value found in table");}}chevron_rightfilter_none - boolean containsValue(Object value) : returns true if this hash table maps one or more keys to this value.
Syntax :public boolean containsValue(Object value) Returns :returns true if this map maps one or more keys to the specified value. Exception :NullPointerException is thrown if the value is null.
// Java code illustrating containsValue() methodimportjava.util.*;classhashTabledemo {publicstaticvoidmain(String[] arg){// creating a hash tableHashtable marks =newHashtable();// enter name/marks pairmarks.put("tweener",newInteger(345));marks.put("krantz",newDouble(245.78));marks.put("burrows",newInteger(790));marks.put("tancredi",newDouble(365.98));marks.put("bellick",newInteger(435));// check whether a value exists or notif(marks.containsValue(345))System.out.println("value found in table");}}chevron_rightfilter_noneOutput:
value found in table
- Enumeration elements() :Returns an enumeration of the values obtained in hash table.
Syntax :public Enumeration elements() Returns :returns an enumeration of the values in this hash table. Exception :NA<
// Java code illustrating elements() methodimportjava.util.*;classhashTabledemo {publicstaticvoidmain(String[] arg){// creating a hash tableHashtable h =newHashtable();h.put(3,"Geeks");h.put(2,"forGeeks");h.put(1,"isBest");// create enumerationEnumeration e = h.elements();System.out.println("display values:");while(e.hasMoreElements()) {System.out.println(e.nextElement());}}}chevron_rightfilter_noneOutput:
display values: Geeks forGeeks isBest
- entrySet() : used to get a set view of the entries contained in this hash table.
Syntax :public Set<Map.Entry> entrySet() Returns :returns a set view of the mappings contained in this map. Exception :NA
// Java code illustrating entreysent() methodimportjava.util.*;classhashTabledemo {publicstaticvoidmain(String[] arg){// creating a hash tableHashtable h =newHashtable();h.put(3,"Geeks");h.put(2,"forGeeks");h.put(1,"isBest");// creating set view for hash tableSet s = h.entrySet();// printing set entriesSystem.out.println("set entries: "+ s);}}chevron_rightfilter_noneOutput:
set entries: [3=Geeks, 2=forGeeks, 1=isBest]
- boolean equals(Object o) : used to compare specified object with this Map for equality.
Syntax :public boolean equals(Object o) Returns :returns true if the specified Object is equal to this Map. Exception :NA
// Java code illustrating equal() methodimportjava.util.*;classhashTabledemo {publicstaticvoidmain(String[] arg){// creating a hash tableHashtable h =newHashtable();Hashtable h1 =newHashtable();h.put(3,"Geeks");h.put(2,"forGeeks");h.put(1,"isBest");h1.put(3,"Geeks");h1.put(2,"forGeeks");h1.put(1,"isBest");// checking whether both hash tables are equal or notif(h.equals(h1))System.out.println("both are equal");}}chevron_rightfilter_noneOutput:
both are equal
- Object get(Object key) : used to get the object that contains the value associated with key.
Syntax :public Object get(Object key) Returns :the value to which the key is mapped in this hashtable. Exception :NullPointerException if the key is null.
// Java code illustrating get() methodimportjava.util.*;classVector_demo {publicstaticvoidmain(String[] arg){// creating a hash tableHashtable marks =newHashtable();// enter name/marks pairmarks.put("tweener",newInteger(345));marks.put("krantz",newDouble(245.78));marks.put("burrows",newInteger(790));marks.put("tancredi",newDouble(365.98));marks.put("bellick",newInteger(435));// get the value mapped with key krantzSystem.out.println(marks.get("krantz"));}}chevron_rightfilter_noneOutput:
245.78
- int hashCode() :returns the hash code value for this Map as per the definition in the Map interface.
Syntax :public int hashCode() Returns :a hash code value for this object. Exception :NA
// Java code illustrating hashCode() methodimportjava.util.*;classhashTabledemo {publicstaticvoidmain(String[] arg){// creating a hash tableHashtable h =newHashtable();h.put(3,"Geeks");h.put(2,"forGeeks");h.put(1,"isBest");// obtaining hash codeSystem.out.println("hash code is: "+ h.hashCode());}}chevron_rightfilter_noneOutput:
hash code is: -672864097
- boolean isEmpty() :used to test if this hashtable maps no keys to values.
Syntax :public boolean isEmpty() Returns :true if this hashtable maps no keys to values; false otherwise. Exception :NA
// Java code illustrating isEmpty() methodimportjava.util.*;classhashTabledemo {publicstaticvoidmain(String[] arg){// creating a hash tableHashtable h =newHashtable();h.put(3,"Geeks");h.put(2,"forGeeks");h.put(1,"isBest");// clear hash table hh.clear();// checking whether hash table h is empty or notif(h.isEmpty())System.out.println("yes hash table is empty");}}chevron_rightfilter_noneOutput:
yes hash table is empty
- Enumeration keys() :used to get enumeration of the keys contained in the hash table.
Syntax :public Enumeration keys() Returns :an enumeration of the keys in this hashtable. Exception :NA
// Java code illustrating keys() methodimportjava.util.*;classhashTabledemo {publicstaticvoidmain(String[] arg){// creating a hash tableHashtable h =newHashtable();Hashtable h1 =newHashtable();h.put(3,"Geeks");h.put(2,"forGeeks");h.put(1,"isBest");// create enumerationEnumeration e1 = h.keys();System.out.println("display key:");while(e1.hasMoreElements()) {System.out.println(e1.nextElement());}}}chevron_rightfilter_noneOutput:
display key: 3 2 1
- Object put(Object key, Object value) :maps the specified key to the specified value in this hashtable.
Syntax :public Object put(Object key, Object value) Returns : returns null if key is not already in the hash table; returns the previous value associated with key if key is already in the hash table. Exception :NullPointerException if the key or value is null.
// Java code illustrating put() methodclasshashTabledemo {publicstaticvoidmain(String[] arg){// creating a hash tableHashtable h =newHashtable();// key/value pairh.put(3,"Geeks");h.put(2,"forGeeks");h.put(1,"isBest");System.out.println("entries in table: "+ h);}}chevron_rightfilter_noneOutput:
entries in table: {3=Geeks, 2=forGeeks, 1=isBest} - putIfAbsent(Key, Function): The putIfAbsent(Key, value) method of Hashtable class which allows to map a value to a given key if given key is not associated with a value or mapped to null. A null value is returned if such key-value set is already present in the HashMap.
Syntax: public <K, V> computeIfAbsent(K key, V value) Parameters: This method accepts two parameters:- key: specifies the key to which the specified value is to be mapped if key is not associated with any value.
- value: specifies the value to be mapped to the specified key.
Below programs illustrate the putIfAbsent(Key, Value) method:
// Java program to demonstrate// putIfAbsent(key, value) method.importjava.util.*;publicclassGFG {// Main methodpublicstaticvoidmain(String[] args){// crete a table and add some valuesMap<String, Integer>table =newHashtable<>();table.put("Pen",10);table.put("Book",500);table.put("Clothes",400);table.put("Mobile",5000);// print map detailsSystem.out.println("hashTable: "+ table.toString());// Inserting non-existing key with value// using putIfAbsent methodString retValue= String.valueOf(table.putIfAbsent("Booklet",2500));// Print the returned valueSystem.out.println("Returned value "+"for Key 'Booklet' is: "+ retValue);// print new mappingSystem.out.println("hashTable: "+ table);// Inserting existing key with value// using putIfAbsent methodretValue= String.valueOf(table.putIfAbsent("Book",4500));// Print the returned valueSystem.out.println("Returned value"+" for key 'Book' is: "+ retValue);// print new mappingSystem.out.println("hashTable: "+ table);}}chevron_rightfilter_noneOutput:
hashTable: {Book=500, Mobile=5000, Pen=10, Clothes=400} Returned value for Key 'Booklet' is: null hashTable: {Book=500, Mobile=5000, Pen=10, Clothes=400, Booklet=2500} Returned value for key 'Book' is: 500 hashTable: {Book=500, Mobile=5000, Pen=10, Clothes=400, Booklet=2500} - KeySet() :used to get a Set view of the keys contained in this hash table.
Syntax :public Set keySet() Returns :a set view of the keys contained in this map. Exception :NA
// Java code illustrating keySet() methodimportjava.util.*;classhashTabledemo {publicstaticvoidmain(String[] arg){// creating a hash tableHashtable h =newHashtable();h.put(3,"Geeks");h.put(2,"forGeeks");h.put(1,"isBest");// creating set view for keysSet sKey = h.keySet();// checking key setSystem.out.println("key set: "+ sKey);}}chevron_rightfilter_noneOutput:
key set: [3, 2, 1]
- void putAll(Map t) : copies all of the mappings from the specified map to this hashtable.
Syntax :public void putAll(Map t) Returns :NA Exception :NullPointerException if the specified map is null.
// Java code illustrating putAll() methodimportjava.util.*;classhashTabledemo {publicstaticvoidmain(String[] arg){// creating a hash tableHashtable h =newHashtable();Hashtable h1 =newHashtable();h.put(3,"Geeks");h.put(2,"forGeeks");h.put(1,"isBest");// copy all element of h into h1h1.putAll(h);// checking h1System.out.println("Values in h1: "+ h1);}}chevron_rightfilter_noneOutput:
Values in h1: {3=Geeks, 2=forGeeks, 1=isBest} </pre - protected void rehash() : Increase the size of the hash table and rehashes all its keys.
Syntax :protected void rehash() Returns :NA Exception :NA
- Object remove(Object key) :Removes key and its value.
Syntax :public Object remove(Object key) Returns :returns the value associated with key. If key is not in the hash table, a null object is returned. Exception :NullPointerException id specified key is null.
// Java code illustrating remove() methodimportjava.util.*;classhashTabledemo {publicstaticvoidmain(String[] arg){// creating a hash tableHashtable h =newHashtable();h.put(3,"Geeks");h.put(2,"forGeeks");h.put(1,"isBest");// remove value for 2 from Hashtable hh.remove(2);// checking Hashtable hSystem.out.println("values after remove: "+ h);}}chevron_rightfilter_noneOutput:
values after remove: {3=Geeks, 1=isBest} - int size() :returns the number of entries in hash table.
Syntax :public int size() Returns :returns the number of keys in this hashtable. Exception :NA
// Java code illustrating size() methodimport.java.util.*;classhashTabledemo {publicstaticvoidmain(String[] arg){// creating a hash tableHashtable marks =newHashtable();// enter name/marks pairmarks.put("tweener",newInteger(345));marks.put("krantz",newDouble(245.78));marks.put("burrows",newInteger(790));marks.put("tancredi",newDouble(365.98));marks.put("bellick",newInteger(435));// size of hash tableSystem.out.println("Size is: "+ marks.size());}}chevron_rightfilter_noneOutput:
Size is: 5
- String toString() :returns the string equivalent of a hash table.
Syntax :public String toString() Returns : Exception :NA
// Java code illustrating toString() methodimportjava.util.*;classhashTabledemo {publicstaticvoidmain(String[] arg){// creating a hash tableHashtable h =newHashtable();h.put(3,"Geeks");h.put(2,"forGeeks");h.put(1,"isBest");// String equivalent of mapSystem.out.println("string equivalent of map: "+ h.toString());}}chevron_rightfilter_noneOutput:
string equivalent of map: {3=Geeks, 2=forGeeks, 1=isBest} - values() :used to get a Collection view of the values contained in this Hashtable.
Syntax :public Collection values() Returns :returns a collection view of the values contained in this map. Exception :NA
// Java code illustrating values() methodimportjava.util.*;classhashTabledemo {publicstaticvoidmain(String[] arg){// creating a hash tableHashtable h =newHashtable();h.put(3,"Geeks");h.put(2,"forGeeks");h.put(1,"isBest");// creating set view for hash tableSet s = h.entrySet();// checking collection view of valuesSystem.out.println("collection values: "+ h.values());}}chevron_rightfilter_noneOutput:
collection values: [Geeks, forGeeks, isBest]
Output:
value found in table
This article is contributed by Abhishek Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Hashtable put() Method in Java
- Hashtable get() Method in Java
- Hashtable contains() Method in Java
- Hashtable containsKey() Method in Java
- Hashtable keys() Method in Java
- Hashtable containsValue() Method in Java
- Hashtable isEmpty() Method in Java
- Hashtable elements() Method in Java
- Differences between HashMap and HashTable in Java
- Hashtable toString() Method in Java
- Hashtable size() Method in Java
- Hashtable remove() Method in Java
- Hashtable clone() Method in Java
- Hashtable clear() Method in Java
- HashTable forEach() method in Java with Examples



