• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Collections and Generics: nested interfaces possible?  RSS feed

 
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to construct a nested data structure using collections and generics, and am having trouble using interfaces instead of implementations at the deeper levels. What I'd like to do is this:

but the compiler forces me to use a concrete implementation for the nested Map:

This works fine, but is there a way to use the interface instead of the implementation, in case I want to use a different one later? There's the "? extends X" syntax for subclasses, but I don't see something similar for interfaces.

Taking the question further, is there an introduction into these finer points of generics? The Sun Java Tutorial doesn't seem to go that far.
 
Sheriff
Posts: 21423
94
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What compiler are you using? Because the following code compiles perfectly in Java 1.6:

Even if I declare map of being a HashMap instead of a Map it compiles.
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, I have no problem compiling and executing this line:

Do you get a different result? I'm using JDK 6 update 3 - how about you?
 
Ulf Dittmer
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using Java 5, which does indeed compile the code you posted.

After some more fiddling with the types, it now seems to work the way I intended it to in the first place.

Thanks for your help. Looks like the problem was actually an InsufficientCaffeineError on my part.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!