Java Collection Tutorial - Java Hashtable.put(K key, V value)








Syntax

Hashtable.put(K key, V value) has the following syntax.

public V put(K key, V value)

Example

In the following code shows how to use Hashtable.put(K key, V value) method.

//w  w w.j  av  a2 s  . c o m
import java.util.Hashtable;

public class Main {
   public static void main(String args[]) {
      // create hash table 
      Hashtable<Integer,String> htable1 = new Hashtable<Integer,String>();      
      
      // put values in table
      htable1.put(1, "A");
      htable1.put(2, "B");
      htable1.put(3, "C");
      htable1.put(4, "from java2s.com");
      
      System.out.println("Initial hash table value: "+htable1);
      
      String returnval=(String)htable1.put(1,"C");
      
      System.out.println("Return value: "+returnval);
      
      System.out.println("New hash table value: "+htable1); 
   }    
}

The code above generates the following result.