File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Collections and Generics: nested interfaces possible? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Collections and Generics: nested interfaces possible?" Watch "Collections and Generics: nested interfaces possible?" New topic
Author

Collections and Generics: nested interfaces possible?

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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?


"I'm not back." - Bill Harding, Twister
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collections and Generics: nested interfaces possible?
 
Similar Threads
Collections / Generics
Generics
generics
Generics
Generics