posted 15 years ago
I do not understand why the following code (and any plausible variation on it) refuses to compile
if (bean instanceof Map<?, ?>) {
Map<?,?> map = ((Map<?,?>) bean); // works
Set<Map.Entry<?, ?>> set = map.entrySet(); // error
// ...
}
Type mismatch: cannot convert from Set<Map.Entry<capture#4-of ?,capture#5-of ?>> to Set<Map.Entry<?,?>>
Only when I use the following sledge hammer (just short of doing completely away with generics), do I get the code to compile:
if (bean instanceof Map<?, ?>) {
Map<?,?> map = ((Map<?,?>) bean); // works
Set<?> set = map.entrySet(); // works
// ...
}
But then, of course, I cannot assume that my set consists of Map.Entry elements. I have tried variations using "<? extends Object" or simply "<Object", but it all refuses to compile.
So what syntax should I use to give my code a cool generic glow?