Two Laptop Bag
The moose likes Java in General and the fly likes Generic Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generic Issue" Watch "Generic Issue" New topic

Generic Issue

Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Hey guys,

I was wondering if the following is possible using generics:

What i have now:

what I want:

So, the question pretty much is whether or not there is a way to tell the map that it's values are restricted to the type of the key parameter.


JDBCSupport - An easy to use, light-weight JDBC framework -
Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

No, I don't think so. I think you're pretty stuck there without writing your own Map-like structure. Generic restrictions are designed to apply to all objects in the collection, whereas you want to have the restriction depend on the position in the collection.

You could always write your own wrapper class. I'm not sure how complicated it would get, but it would start off something like this:
Whether that's worth the trouble, I'm unsure.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
thanks for your reply ...

the structure is fully encapsulated so there is no real problem. i just figured it would be a nicer way to do it like that :-)
I agree. Here's the link:
subject: Generic Issue
jQuery in Action, 3rd edition