If you’re actually using a hash, this is what you really want to do:
import java.util.HashMap; import java.util.Iterator; import java.util.Map; @SuppressWarnings({ "unchecked", "serial" }) public class DrSuess { public static void main(String[] args) { Map foo = new HashMap() { { put(1, "fish"); put(2, "fish"); put("red", "fish"); put("blue", "fish"); } }; Iterator i = foo.keySet().iterator(); while(i.hasNext()) { Object key = i.next(); System.out.println(key + " " + foo.get(key)); } } }
but generics frowns on it. A Dictionary could be <String, String> or an AssociativeArray could be <String, Object> but a Hash wants to be able to put anything in it. Give me useful subclasses, not the useless crap that is Java generics.