Hashtable put() Method in Java
The java.util.Hashtable.put() method of Hashtable is used to insert a mapping into a table. This means we can insert a specific key and the value it is mapping to into a particular table. If an existing key is passed then the previous value gets replaced by the new value. If a new pair is passed, then the pair gets inserted as a whole.
Syntax:
Hash_Table.put(key, value)
Parameters: The method takes two parameters, both are of the Object type of the Hashtable.
- key: This refers to the key element that needs to be inserted into the Table for mapping.
- value: This refers to the value that the above key would map into.
Return Value: If an existing key is passed then the previous value gets returned. If a new pair is passed, then NULL is returned.
Below programs are used to illustrate the working of java.util.Hashtable.put() Method:
Program 1: When passing an existing key.
// Java code to illustrate the put() method import java.util.*; public class Hash_Table_Demo { public static void main(String[] args) { // Creating an empty Hashtable Hashtable<Integer, String> hash_table = new Hashtable<Integer, String>(); // Inserting values into the table hash_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 Hashtable System.out.println("Initial table is: " + hash_table); // Inserting existing key along with new value String returned_value = (String)hash_table.put(20, "All"); // Verifying the returned value System.out.println("Returned value is: " + returned_value); // Displaying the new table System.out.println("New table is: " + hash_table); } } |
Initial table is: {10=Geeks, 20=Geeks, 30=You, 15=4, 25=Welcomes}
Returned value is: Geeks
New table is: {10=Geeks, 20=All, 30=You, 15=4, 25=Welcomes}
Program 2: When passing a new key.
// Java code to illustrate the put() method import java.util.*; public class Hash_Table_Demo { public static void main(String[] args) { // Creating an empty Hashtable Hashtable<Integer, String> hash_table = new Hashtable<Integer, String>(); // Inserting values into the table hash_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 Hashtable System.out.println("Initial table is: " + hash_table); // Inserting existing key along with new value String returned_value = (String)hash_table.put(50, "All"); // Verifying the returned value System.out.println("Returned value is: " + returned_value); // Displaying the new table System.out.println("New table is: " + hash_table); } } |
Initial table is: {10=Geeks, 20=Geeks, 30=You, 15=4, 25=Welcomes}
Returned value is: null
New table is: {10=Geeks, 20=Geeks, 30=You, 50=All, 15=4, 25=Welcomes}
Note: The same operation can be performed with any type of variation and combination of different data types.
Recommended Posts:
- Hashtable get() Method in Java
- Hashtable contains() Method in Java
- Hashtable clear() Method in Java
- Hashtable clone() Method in Java
- Hashtable remove() Method in Java
- Hashtable keys() Method in Java
- Hashtable size() Method in Java
- Hashtable containsValue() Method in Java
- Hashtable containsKey() Method in Java
- Hashtable elements() Method in Java
- Hashtable isEmpty() Method in Java
- Hashtable toString() Method in Java
- HashTable compute() method in Java with Examples
- Hashtable computeIfAbsent() method in Java with Examples
- HashTable forEach() method in Java with Examples
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



