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?