Hashtable put() Method in Java - GeeksforGeeks

28 Jun, 2018
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() methodimport 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() methodimport 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.
Java-Collections
Java-HashTable
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Arrays.sort() in Java with examples
Reverse a string in Java
Initialize an ArrayList in Java
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Interfaces in Java
How to iterate any Map in Java