Hashtables in Java Programming
Hashtables in Java Programming
Declaration:
public class Hashtable<K,V> extends Dictionary<K,V> implements
Map<K,V>, Cloneable, Serializable
Type Parameters:
● K – the type of keys maintained by this map
● V – the type of mapped values
Constructors:
● Java
import java.io.*;
import java.util.*;
class AddElementsToHashtable {
{
ht1.put(1, "one");
ht1.put(2, "two");
ht1.put(3, "three");
ht2.put(4, "four");
ht2.put(5, "five");
ht2.put(6, "six");
}
Output
Mappings of ht1 : {3=three, 2=two, 1=one}
Mappings of ht2 : {6=six, 5=five, 4=four}
2. Hashtable(int initialCapacity): This creates a hash table that has an initial
size specified by initialCapacity .
● Java
import java.io.*;
import java.util.*;
class AddElementsToHashtable {
{
ht1.put(1, "one");
ht1.put(2, "two");
ht1.put(3, "three");
ht2.put(4, "four");
ht2.put(5, "five");
ht2.put(6, "six");
}
Output
Mappings of ht1 : {3=three, 2=two, 1=one}
Mappings of ht2 : {4=four, 6=six, 5=five}
3. Hashtable(Map<? extends K,? extends V> m): This creates a hash table
that is initialized with the elements in m.
Hashtable<K, V> ht = new Hashtable<K, V>(Map m);
● Java
import java.io.*;
import java.util.*;
Continue from down below here next lesson
class AddElementsToHashtable {
{
hm.put(1, "one");
hm.put(2, "two");
hm.put(3, "three");
}
Output
Mappings of ht2 : {3=three, 2=two, 1=one}
Example:
● Java
// Java.util.Hashtable
import java.util.*;
{
// Add elements to the hashtable
ht.put("vishal", 10);
ht.put("sachin", 30);
ht.put("vaibhav", 20);
System.out.println(ht);
if (ht.containsKey("vishal")) {
Integer a = ht.get("vishal");
}
}
Output
Size of map is:- 3
{vaibhav=20, vishal=10, sachin=30}
value for key "vishal" is:- 10
Performing Various Operations on Hashtable
1. Adding Elements: In order to add an element to the hashtable, we can use
the put() method. However, the insertion order is not retained in the hashtable.
Internally, for every element, a separate hash is generated and the elements are
indexed based on this hash to make it more efficient.
● Java
import java.io.*;
import java.util.*;
class AddElementsToHashtable {
{
// Initialization of a Hashtable
ht1.put(1, "Geeks");
ht1.put(2, "For");
ht1.put(3, "Geeks");
ht2.put(1, "Geeks");
ht2.put(2, "For");
ht2.put(3, "Geeks");
}
}
Output
Mappings of ht1 : {3=Geeks, 2=For, 1=Geeks}
Mappings of ht2 : {3=Geeks, 2=For, 1=Geeks}
2. Changing Elements: After adding the elements if we wish to change the
element, it can be done by again adding the element with the put() method. Since
the elements in the hashtable are indexed using the keys, the value of the key
can be changed by simply inserting the updated value for the key for which we
wish to change.
● Java
// updating Hashtable
import java.io.*;
import java.util.*;
class UpdatesOnHashtable {
{
Hashtable<Integer, String> ht
= new Hashtable<Integer, String>();
ht.put(1, "Geeks");
ht.put(2, "Geeks");
ht.put(3, "Geeks");
ht.put(2, "For");
}
Output
Initial Map {3=Geeks, 2=Geeks, 1=Geeks}
Updated Map {3=Geeks, 2=For, 1=Geeks}
3. Removing Element: In order to remove an element from the Map, we can use
the remove() method. This method takes the key value and removes the
mapping for a key from this map if it is present in the map.
● Java
import java.io.*;
import java.util.*;
class RemovingMappingsFromHashtable {
{
Map<Integer, String> ht
ht.put(2, "For");
ht.put(3, "Geeks");
ht.put(4, "For");
ht.remove(4);
}
Output
Initial map : {4=For, 3=Geeks, 2=For, 1=Geeks}
Updated map : {3=Geeks, 2=For, 1=Geeks}
4. Traversal of a Hashtable: To iterate the table, we can make use of
an advanced for loop. Below is the example of iterating a hashtable.
● Java
// Java program to illustrate
// traversal of Hashtable
import java.util.Hashtable;
import java.util.Map;
{
ht.put("vishal", 10);
ht.put("sachin", 30);
ht.put("vaibhav", 20);
+ e.getValue());
}
Output
vaibhav 20
vishal 10
sachin 30
import java.util.*;
System.out.println(
"Contains value 'practice.geeks"
+ "forgeeks.org'? "
+ lhm.containsValue("practice"
+ ".geeksforgeeks.org"));
Output
{one=practice.geeksforgeeks.org, two=code.geeksforgeeks.org,
four=quiz.geeksforgeeks.org}
Getting value for key 'one': practice.geeksforgeeks.org
Size of the map: 3
Is map empty? false
Contains key 'two'? true
Contains value 'practice.geeksforgeeks.org'? true
delete element 'one': practice.geeksforgeeks.org
Mappings of LinkedHashMap : {two=code.geeksforgeeks.org,
four=quiz.geeksforgeeks.org}
How to sort the Hashtable by values in Java?
The Hashtable class in Java does not maintain the order of its entries, so even if we sort
it, there is no guarantee that the elements will be returned in that order. We need to
convert a hashtable to a collection that maintains the element order.
3 htRandomNumbers.put("Five", 5);
4 htRandomNumbers.put("Three", 3);
5 htRandomNumbers.put("Nine", 9);
6 htRandomNumbers.put("Seven", 7);
7 htRandomNumbers.put("Two", 2);
10
11 /*
14 * to a List
15 */
17
18 /*
21 */
22 Collections.sort(list);
23
Output
The sort method of the Collections class sorts the list elements in their natural order,
which is the ascending order for the type Integer. If you want to sort the values in
descending order, pass the reverse comparator in the sort method as given below.
1 Collections.sort(list, Collections.reverseOrder());
Output
Approach:
1. Take LinkedHashMap as an input.
2. Create a new TreeMap.
3. Pass LinkedHashMap object into the constructor of TreeMap.
4. Print Keys of TreeMap object.
● Java
// Sort LinkedHashMap by keys in Java
import java.util.*;
import java.io.*;
class GFG {
public static void main(String[] args)
{
LinkedHashMap<Integer, Integer> lMap
= new LinkedHashMap<>();
// convert to TreeMap
Map<Integer, Integer> map = new
TreeMap<>(lMap);
Output
After Sorting :