How to Add or Update Elements in a HashTable in Java? Last Updated : 23 Jul, 2025 Suggest changes Share 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. S seepanarajvskq Follow 0 Article Tags : Java Java Programs Java - util package HashTable Java-HashTable Java Examples +2 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like