Mega Code Archive

 
Categories / Java Tutorial / Collections
 

Using the elements() or the values() method to get the set of all the values in the hash table

public Enumeration elements() public Collection values() The elements() method returns the set of values as an Enumeration. The values() method returns the same data as a Collection. This method returns a Collection instead of a Set because the values may contain duplicates. import java.util.Enumeration; import java.util.Hashtable; public class MainClass {   public static void main(String[] s) {     Hashtable table = new Hashtable();     table.put("key1", "value1");     table.put("key2", "value2");     table.put("key3", "value3");     Enumeration e = table.elements();     while (e.hasMoreElements()) {       String key = (String) e.nextElement();       System.out.println(key + " : " + table.get(key));     }     System.out.println(table.values());   } } value3 : null value2 : null value1 : null [value3, value2, value1]