• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collections and Generics: nested interfaces possible?

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22783
131
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
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic