Hashtable implements Map . The Map.entrySet function returns a collection ( Set ) of Map.Entry instances that have the getKey and getValue .
So:
Iterator<Map.Entry> it; Map.Entry entry; it = yourTable.entrySet().iterator(); while (it.hasNext()) { entry = it.next(); System.out.println( entry.getKey().toString() + " " + entry.getValue().toString()); }
If you know the types of entries in a Hashtable, you can use templates to eliminate the above toString calls. For example, entry could be declared Map.Entry<String,String> if your Hashtable is declared Hashtable<String,String> .
If you can combine templates with generics, this is just short:
for (Map.Entry<String,String> entry : yourTable.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue()); }
This assumes yourTable is Hashtable<String,String> . It just goes to show how far Java has gone over the past few years, in many ways, without losing its substantial Java version.
Slightly OT: If you don't need synchronization, use a HashMap instead of a Hashtable . If you do, use ConcurrentHashMap (thanks, akappa!).
Tj crowder
source share