aspose file tools*
The moose likes Beginning Java and the fly likes Generics : Passing a Map of <String, List<something extending a superclass>> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generics : Passing a Map of <String, List<something extending a superclass>>" Watch "Generics : Passing a Map of <String, List<something extending a superclass>>" New topic
Author

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

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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 :


[My Blog]
All roads lead to JavaRanch
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1778
    
    7

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.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Mar 10, 2008
Posts: 1778
    
    7

Maybe this explanation will help:

http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeArguments.html#FAQ104
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1778
    
    7

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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Nov 24, 2005
Posts: 14688
    
  16

Ok, it's solved. I returned the value I wanted to put in the map. Thank you very much.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Thanks Rob.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics : Passing a Map of <String, List<something extending a superclass>>