I think the short answer is that Java allows for "uncontrolled selection" in some situations, but not in others. Working with raw types (generic types without a specified type) is one of these instances.
Keep in mind that for (Map.Entry entry : set) equivalent to:
Iterator it = set.iterator(); while (it.hasNext()) { Map.Entry entry = it.next(); }
Appointment:
Set set = map.entrySet();
it is allowed and will not generate any warnings, since you are not introducing any new type, but in the for it.next() loop it will return the Object type and you will get a compiler exception if you assigned it without an explicit cast.
Appointment:
Set <Map.Entry> set = map.entrySet();
allowed, but it will generate an “unchecked throw” warning due to the explicit type Map.Entry and in the for it.next() loop will return the type Map.Entry , and the destination will work fine.
You can put an explicit conversion into a for loop as follows:
for(Map.Entry entry : (Set<Map.Entry>) map.entrySet())
source share