This example shows how to get all the entries (mapping) from the Hashtable in Java. This example also shows how to get all the entries of Hashtable using the entrySet method.
How to get all entries from the Hashtable in Java (all key-value mappings)?
The Hashtable entrySet
method returns all the entries stored in the hashtable object.
1 |
public Set<Map.Entry<K,V>> entrySet() |
The entrySet
method returns a Set view of all the entries or key-value mappings stored in the hashtable object.
Once we get the entry set using the entrySet
method, we can get an iterator from it using the iterator
method of the Set. We can then iterate through all the entries of the hashtable using the hasNext
and next
methods of the iterator.
Each entry in the entry set is an object of the Map.Entry class and represents a single key-value mapping contained in the hashtable object. We can get the key from the entry using the getKey
method and value using the getValue
method 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 27 28 29 30 31 32 33 34 35 36 37 38 |
import java.util.Hashtable; import java.util.Iterator; import java.util.Map; import java.util.Set; public class HashtableGetAllEntries { public static void main(String[] args) { Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); /* * To get all entries or mappings of the hashtable * using the entrySet method. */ Set<Map.Entry<Integer, String>> entryset = hashtable.entrySet(); //get an iterator Iterator<Map.Entry<Integer, String>> itr = entryset.iterator(); //iterate Map.Entry<Integer, String> currentEntry = null; while( itr.hasNext() ){ currentEntry = itr.next(); /* * Get key from an entry using the getKey method and * get value from an entry using the getValue method */ System.out.println( currentEntry.getKey() + " -> " + currentEntry.getValue() ); } } } |
Output
1 2 3 |
3 -> Three 2 -> Two 1 -> One |
Important Note:
The entrySet
method returns a view that is backed by the original hashtable object. That means any changes we make to this set will also be reflected in the original hashtable object, and vice versa.
Let’s see an example of that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); Iterator<Map.Entry<Integer, String>> itr = hashtable.entrySet().iterator(); Map.Entry<Integer, String> currentEntry = null; while( itr.hasNext() ){ currentEntry = itr.next(); //Change entry from 2=>Two to 2=>Twenty if(currentEntry.getKey().equals(2)){ currentEntry.setValue("Twenty"); } } System.out.println("Hashtable contains: " + hashtable); |
Output
1 |
Hashtable contains: {3=Three, 2=Twenty, 1=One} |
As we can see from the output when we changed the entry value for the key 2, the change was reflected in the hashtable object as well.
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