How to Add or Update Elements in a HashTable in Java? Last Updated : 07 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In Java, HashTable is a pre-defined class of the collection framework. It is a part of the java.util package and is used to store the key-value pairs in the program. In this article, we will learn how to add and update elements in a HashTable in Java. Pre-defined Classe to add and update elements in a HashTableTo add or update elements in a HashTable the pre-defined class is: HashTable: It is a pre-defined class and it can be used to add or update the elements into the HashMap using the HashTable.Program to add and update elements in a HashTable in JavaBelow is the code implementation to add or update elements in a HashTable. Java // Java program to add or update elements in a HashTable import java.util.Hashtable; import java.util.Map; public class GFGHashTableExample { public static void main(String[] args) { // create a HashTable named as hashTable Hashtable<String, String> hashTable = new Hashtable<>(); // adding elements hashTable.put("key1", "HTML"); hashTable.put("key2", "CSS"); hashTable.put("key3", "JavaScript"); hashTable.put("key4", "ReactJs"); // printing the elements before update System.out.println("Original HashMap"); for (Map.Entry<String, String> entry : hashTable.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } // updating an element hashTable.put("key3", "NodeJs"); // Printing the elements after update System.out.println(); System.out.println("Updated HashMap"); for (Map.Entry<String, String> entry : hashTable.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } } } OutputOriginal HashMap key4: ReactJs key3: JavaScript key2: CSS key1: HTML Updated HashMap key4: ReactJs key3: NodeJs key2: CSS key1: HTML Explanation of the Program:The above Java program demonstrates adding and updating elements in a Hashtable. We have created a Hashtable named hashTable and adds key-value pairs into it. Then the original Hashtable is printed. After that, it updates the value associated with the key "key3" to "NodeJs" and prints the Hashtable again to show the changes. Comment More infoAdvertise with us Next Article How to Add or Update Elements in a HashTable in Java? S seepanarajvskq Follow Improve Article Tags : Java Java Programs Java - util package HashTable Java-HashTable Java Examples +2 More Practice Tags : Java Similar Reads How to Use Enumeration to Display Elements of Hashtable in Java? Hashtable 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. Now here we can get the keys and 2 min read How to Eliminate Duplicate Keys in Hashtable in Java? HashTable class is part of the Collection framework in Java where the only major difference it has from HashMap is that it's synchronized. Hash table maps keys to values i.e. it internally uses buckets to store key-value pairs and the corresponding bucket to a key-value pair is determined by the key 4 min read How to Convert a HashTable to Other Collections Types in Java? In Java, a Hashtable is a data structure that stores the data in the form of key and value pairs. And each key is mapped to a specific value. It implements the Map interface. HashTable provides a simple way to access the data using the unique keys. In this article, we will learn how to convert a Has 3 min read How to Create a Synchronized HashTable in Java? In Java, a synchronized HashTable is achieved by wrapping a regular HashTable with the Collection.synchronizedMap( ) method. This wrapper ensures that each method of the Map interface is synchronized, making the HashTable thread-safe. Syntax:Map<KeyType, ValueType> synchronizedHashTable = Coll 3 min read How to Create a HashSet with a Custom Initial Load Factor in Java? Java HashSet is a simple data structure provided by the Java Collection Framework that provides efficient storage and enables the storage of unique objects. One of the parameters that affect its performance is the load factor, which determines when the underlying hash table should be updated to acco 2 min read Like