• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics : Passing a Map of <String, List<something extending a superclass>>

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need some Generics enlightenment. I want to pass a Map of <String, List><something extending superclass>>,
but I can't figure out how. Here is a sample :
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes, nested generic collections and wildcards, that's always fun...
Try this (by the way the code you posted has some syntax errors due to misplaced '>' characters):



Edit: Arggh, the forum software insists on messing with the < and > characters, but you get the idea.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by the way the code you posted has some syntax errors due to misplaced '<' characters

It looks like the code tag is messing things up. Your code looks messed up too, but I got it to compile. Thank you.
I don't understand the concept though. Why does "? extends List" solve it ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another problem is that in doSomethingListMap, I want to put values (which extend SuperClass) in the Map. For example, map.put("aa", new ChildOne()); The compiler doesn't let me do that.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe this explanation will help:

http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeArguments.html#FAQ104
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:Another problem is that in doSomethingListMap, I want to put values (which extend SuperClass) in the Map. For example, map.put("aa", new ChildOne()); The compiler doesn't let me do that.


That's because the compiler won't allow you to add elements to a collection that uses an extends bounded wildcard, it becomes read-only for type-safety reasons.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:Maybe this explanation will help:
http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeArguments.html#FAQ104

That's because the compiler won't allow you to add elements to a collection that uses an extends bounded wildcard, it becomes read-only for type-safety reasons.

Thank you, it explains my second problem.
But not how to solve it I'm going to make it non-generic if it gets too much on my nerve.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, it's solved. I returned the value I wanted to put in the map. Thank you very much.
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:That's because the compiler won't allow you to add elements to a collection that uses an extends bounded wildcard, it becomes read-only for type-safety reasons.

In general:

"? super SuperClass": the ? is SuperClass or any super type (e.g. Object), so you can add anything that IS-A SuperClass. You don't know what it contains, so you can only retrieve elements as Object without casting.

"? extends SuperClass": the ? is SuperClass or any sub type. Since you don't know which type, you cannot add anything. You know that everything it contains IS-A SuperClass so you can retrieve elements as SuperClass without casting.


So basically, if you need to add elements to a List or Map, "? super SuperType" is required. If you also need to retrieve elements as SuperType there is no other option than use "SuperType" itself.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic