Get a Collection of values contained in Hashtable - Java Collection Framework

Java examples for Collection Framework:Hashtable

Description

Get a Collection of values contained in Hashtable

Demo Code

  
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Hashtable;
import java.util.Collection;
 
public class Main {
  public static void main(String[] args) {
    Hashtable ht = new Hashtable();
   /*from w  w w  .  j a  v  a2 s .co m*/
    //add key value pairs to Hashtable
    ht.put("1","One");
    ht.put("2","Two");
    ht.put("3","Three");
   
    Collection c = ht.values();
   
    System.out.println("Values of Collection created from Hashtable are :");

    Iterator itr = c.iterator();
    while(itr.hasNext())
      System.out.println(itr.next());
     
    c.remove("One");
   
    System.out.println("Hashtable values after removal from Collection are :");
    Enumeration e = ht.elements();
    while(e.hasMoreElements())
      System.out.println(e.nextElement());
  }
}
 

Result


Related Tutorials