This example shows how to get all values stored in the Hashtable in Java. This example also shows how to get all Hashtable values using the elements method and values method.
How to get all the values of Hashtable in Java?
There are a couple of ways using which we can get all the values stored in the hashtable object.
1. Using the elements method
The Hashtable elements
method returns an Enumeration over the hashtable values.
1 |
public Enumeration<V> elements() |
Once we get the enumeration, we can iterate through all the values of the hash table using the hasMoreElements
and nextElement
methods as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.Enumeration; import java.util.Hashtable; public class HashtableGetAllValuesExample { public static void main(String[] args) { Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(10, "One"); hashtable.put(20, "Two"); /* * To get all values stored in the hashtable * use the elements method */ Enumeration<String> values = hashtable.elements(); //iterate the values while( values.hasMoreElements() ){ System.out.println( values.nextElement() ); } } } |
Output
1 2 3 4 |
One Two Two One |
2. Using the values method
The Hashtable values
method returns a Collection view of all the values contained in the hashtable object.
1 |
public Collection<V> values() |
The Collection object is a view that is backed by the original hashtable object. That means any changes you make to this view will also be reflected in the original hashtable object, and vice versa.
We can get an iterator over values using the iterator
method of the Collections. Once we get an iterator, we can iterate through the values using the hasNext
and next
methods as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(10, "One"); hashtable.put(20, "Two"); /* * To get a Collection view of all the values * stored in the hashtable object, use the * values method */ Collection<String> values = hashtable.values(); //get an iterator Iterator<String> itr = values.iterator(); //iterate while( itr.hasNext() ){ System.out.println( itr.next() ); } |
Output
1 2 3 4 |
One Two Two One |
The elements vs values method
- The
elements
method returns an Enumeration while thevalues
method returns a Collection view. - We can directly iterate over elements using the Enumeration while we need to get the iterator from Collection object to iterate through elements
- The
elements
method is inherited from the JDK 1.0 Dictionary class while thevalues
method was introduced in Java 2.0 when the Hashtable class implemented the Map interface. - We can remove the elements while iterating using the iterator which cannot be done using the Enumeration.
This example is a part of the Hashtable in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 Hashtable Documentation