This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • 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: 1951
7
Eclipse IDE Java
  • 0
  • 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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • 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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • 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: 1951
7
Eclipse IDE Java
  • 0
  • 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: 1951
7
Eclipse IDE Java
  • 0
  • 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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • 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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • 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: 20380
45
Chrome Eclipse IDE Java Windows
  • 0
  • 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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic