Excuse me if this was asked before (I'm almost sure) but I can't find the right keywords or dig it up from the pile of unrelated generics questions.
In short
This works:
Object a = null;
Set<Object> b = (Set<Object>)a;
so why does this give a compiler error?
Set<Object> c = null;
Set<Set<Object>> d = (Set<Set<Object>>)c;
and why is this simple workaround valid?
Set<Object> e = null;
Set<?> f = (Set<?>)e;
Set<Set<Object>> g = (Set<Set<Object>>)f;
Long version: (kept for legacy but no longer relevant to the question)
I have a library method that returns a Map<Number, Object>
of a preprocessed JSON message. After manual deep-type-checking, I need to cast and return it as Map<Number, Map<String, Map<String, Object>>>
, expecting an unchecked cast warning which I can ignore because I've just checked it.
However I'm getting the compiler error:
Error:(61, 10) java: Cannot cast from java.util.Map<java.lang.Number,java.lang.Object> to java.util.Map<java.lang.Number,java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>>>
or in a more readable form (based on the error in the IDE):
Inconvertible types; cannot cast 'Map<Number,Object>' to 'Map<Number,Map<String,Map<String,Object>>>'
)
[edit] Changed from a how-to question to a why-question.