• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, it's solved. I returned the value I wanted to put in the map. Thank you very much.
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob.
 
You showed up just in time for the waffles! And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic