How to get all keys/values out of a Hashtable.
If you like to program in Java language, it is good to know there are a number of ways to get all keys/values out of a Hashtable.
Hashtable ht = new Hashtable();
ht.put(“one”,new Integer(1));
ht.put(“two”,new Integer(2));
Enumeration en = ht.elements();
while(en.hasMoreElements()){
System.out.println((Integer)en.nextElement());
}
or
Hashtable ht = new Hashtable();
ht.put(“one”,new Integer(1));
ht.put(“two”,new Integer(2));
Iterator it=hr.keySet().Iterator();
while(it.hasNext()){
System.out.println(hr.get(it.next()));
}
or
Set s = ht.entrySet();
for (Object o:s.toArray()){
System.out.println(o);
}
//only java 5.0
//print one=1 two=2
}
about more , see java.util.Enumeration and java.util.Hashtable
http://forums.whirlpool.net.au/forum-replies-archive.cfm/593239.html
Filed under: Java

Leave a Reply