• Java Data Structures Tutorial

Adding elements to a hash table



The HashTable class provides a method named put() this method accepts key value pairs (objects) as parameters and adds them to the current hash table.

You can add a key-value pair to the current hash table using this method.

Example

import java.util.Hashtable;

public class CreatingHashTable {
   public static void main(String args[]) {
      Hashtable hashTable = new Hashtable();
      hashTable.put("Ram", 94.6);
      hashTable.put("Rahim", 92);
      hashTable.put("Robert", 85);
      hashTable.put("Roja", 93);
      hashTable.put("Raja", 75);
      System.out.println(hashTable);    		  
   }
}

Output

{Rahim = 92, Roja = 93, Raja = 75, Ram = 94.6, Robert = 85}
Advertisements