Skip to main content

Multiple generics type parameter

1 reply [Last post]
xiangya
Offline
Joined: 2003-09-24
Points: 0

I want write code like this:
class AClass {
private Map, Map> map;

List getMyList(T typeA){
return map.get(typeA.getClass()).get(typeA);
}

}
If no this, maybe I write code like this:
//Tn extends TypteA
class AClass {
Map> map1;
Map> map2;
Map> map3;

List getMyList(T typeA){
if (typeA instanceof T1) return map1.get(typeA);
else if (typeA instanceof T2) return map2.get(typeA);
...
}
}

How do I write it cleanly, can anyone give some help?

Regards.

xiangya

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tackline
Offline
Joined: 2003-06-19
Points: 0

I think the (all too easy) trap you are falling into is that you know that a map should map should map from a single object to another. However, at the language level, the Map interface cannot show that. For a random type with two generic parameters, you would not expect to be able to do something similar. In general you would need to swap T for either a real type or a generic parameter of AClass.