• Post Reply Bookmark Topic Watch Topic
  • New Topic

Set of Set with generics  RSS feed

 
Saverio Miroddi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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 ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Garret, why would that make any difference?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • 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 ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saverio, please check your private messages - you can see them by clicking My Profile.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!