HashMap default types for K and V

There is no default type.

The types in Java generics are only for compile-time checking. They are erased at runtime and essentially gone.

Think of generics as a static helper to a) better document your code, and b) enable some limited compile-time checking for type safety.


The type is java.lang.Object.

The for construct takes a type of Iterable and calls its iterator method. Since the Set isn't typed with generics, the iterator returns objects of type Object. These need to be explicitly cast to type Map.Entry.

Map map = new HashMap();
map.put("one", "1st");
map.put("two", new Integer(2));
map.put("three", "3rd");
for (Object o : map.entrySet()) {
    Map.Entry entry = (Map.Entry) o;
    System.out.println(entry.getKey() + " -> " + entry.getValue());
}