Mega Code Archive
Getting elements (key-value pairs) from a Hashtable
public Set entrySet()
The entries in the returned Set are of type Map.Entry.
When working with the Set returned from entrySet(), you already have both the key and value together:
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
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");
Set set = table.entrySet();
Iterator it = set.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
}
key3 : value3
key2 : value2
key1 : value1