• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Set of Set with generics

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(I am posting here, since I had no answers in the intermediate forum)

If I use:

myFunc(new HashSet<Integer>())

where:

myFunc(Set<? extends Object>

all is ok.

If I use:

myFunc(new HashSet<Set<Integer>>())

where:

myFunc(Set<Set<? extends Object>>

the compiler doesn't compile.

Why??
Thank you.
[ August 20, 2007: Message edited by: Saverio J(ava) Miroddi ]
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, this seems to be an esoteric, corner case of Java generics. As far as I can tell, a

Set<Set<?>> setSet;

denotes a Set of heterogeneous Set objects, whereas a Set<Set<Integer>> denotes a Set of homogeneous Set Objects. Therefore the only thing that can be assigned to a Set<Set<?>> is a Set<Set<?>>:



See also What do multi-level wildcards mean? in Angelika Langer's Generics FAQ.
[ August 20, 2007: Message edited by: Garrett Rowe ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what you want is:


Which is functionally the same as:


[ August 20, 2007: Message edited by: Garrett Rowe ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Garret, why would that make any difference?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry Ilja, I'm not sure what you're asking. Are you asking how


is different from


If so then the difference is that in the first declaration, all the Sets that mySet contains are Set<T>, which is to say that they all must have the same generic type parameter. Therefore the following code compiles without any problems:


In the second declaration mySet can contain Sets that have any generic type parameter. The types of the sets need not be homogeneous. So this code will not compile:


However, this will:


Although I too am a little confused as to why the compiler will not match the homogeneous type to the method containing the unbounded wildcard. It seems to me that even though the second declaration implies that the Set need not be homogeneous, that doesn't mean it can't be.

Maybe someone who understands this stuff a little better might chime in.
[ August 21, 2007: Message edited by: Garrett Rowe ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Saverio, please check your private messages - you can see them by clicking My Profile.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Garrett Rowe:
I'm sorry Ilja, I'm not sure what you're asking. Are you asking how


is different from



No - mostly because that's not the code that you used in your previous post...

Now *this one* seems to make sense. Thanks!
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic