Hacker News new | ask | show | jobs
by phillytom 5422 days ago
FYI - there's a potential defect there - keys are not required to be strings so you should lookup the value before you cast.
2 comments

Ideally, whatever your key/value is would have the toString method over ridden to meaningfully represent it. Alternatively, I could write it as

for(Map.Entry<K, V> entry : map.entrySet()){

     System.out.println(entry.getKey());

     System.out.println(entry.getValue());

}
I was relying on the assumption from the original question that the keys and values were all Strings: "...a Hashtable containing String keys and values and printing them all out."