I have the following two classes:
class Key<T1, T2> {}
class Pair<F, S> {}
I want to create a map from Key<T1, T2> to List<Pair<T1, T2>> where T1 and T2 are different for each map’s entry. I am trying to implement it this way:
class KeyToListMap {
private Map<Key<?, ?>, List<Pair<?, ?>>> myItems = new HashMap<Key<?, ?>, List<Pair<?, ?>>>();
public<T1, T2> List<Pair<T1, T2>> getList(Key<T1, T2> key) {
if (!myItems.containsKey(key)) {
myItems.put(key, new ArrayList<Pair<T1, T2>>());
}
return (List<Pair<T1, T2>>) myItems.get(key);
}
}
However I got two type errors:
put(test.Key<?,?>,java.util.List<test.Pair<?,?>>) in java.util.Map<test.Key<?,?>,java.util.List<test.Pair<?,?>>> cannot be applied to (test.Key<T1,T2>,java.util.ArrayList<test.Pair<T1,T2>>)
inconvertible types
found : java.util.List<test.Pair<?,?>>
required: java.util.List<test.Pair<T1,T2>>
How can I express this in Java without resorting to using raw types?
Use
in Map declaration