Monday, March 3, 2014

What is the difference between Hashtable and HashMap?

import java.util.Iterator;
import java.util.Hashtable;
import java.util.Set;

public class HashtableEntriesIterator {

  public static void main(String[] args) {

    // Create a Hashtable and populate it with elements
    Hashtable hashtable = new Hashtable();
    hashtable.put("key_1","value_1");
    hashtable.put("key_2","value_2");
    hashtable.put("key_3","value_3");

    // Get a set of all the entries (key - value pairs) contained in the Hashtable
    Set entrySet = hashtable.entrySet();


    // Obtain an Iterator for the entries Set
    Iterator it = entrySet.iterator();
   
    // Iterate through Hashtable entries
    System.out.println("Hashtable entries : ");
    while(it.hasNext())

System.out.println(it.next());
   
  }
}

No comments:

Post a Comment