While working on interview questions, I have come across below code:
List<Object> list = new ArrayList(); Map<Object, ? super ArrayList> m = new HashMap<Object, ArrayList>(); m.put(1, new Object()); m.put(2, list);
The above two put method’s are throwing compile time error. But, when I add m.put(3, new ArrayList());
it is adding to map with no compile time error.
It is very clear for me that I can add new Object()
as a value in HashMap
because of the map declaration to be of type < ? super ArrayList>
; it means I can add any value that is higher than ArrayList
(i.e. super of ArrayList
) and ArrayList
object too, but not anything below ArrayList
.
This particular concept is very well written in SCJP 6 by Kathy Sierra and Bert Bates and based on that theory and examples I assumed it should do job as I understood. Can someone help me understand the error?
Advertisement
Answer
The type ? super ArrayList
means an unknown type, which has a lower bound of ArrayList
. For example, it could be Object
, but might be AbstractList
or ArrayList
.
The only thing the compiler can know for sure is that the type of the value, although unknown, is guaranteed to be no more specific than ArrayList
, so any object that is an ArrayList
, or a subclass of ArrayList
, may be added.
Also remember: the compiler only goes by the declared type; it ignores the assigned type.
Why put(1, new Object())
fails:
Clearly, Object
is not within bounds.
Why put(1, list)
fails:
The variable is of type List
, which may hold a reference to a LinkedList
, which is not within the required bounds.