Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections and Generics: nested interfaces possible?

 
Ulf Dittmer
Rancher
Posts: 42968
73
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
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.
 
Jim Yingst
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: 42968
73
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic