I have a Map whose keys are of the general type Key<T> , and the values ββare of type List<T> . If the key is an instance of Key<String> , the value must be List<String> , and the same rule applies to any other key-value pairs. I tried the following, but it does not compile:
Map<T, List<T>> map;
Currently, I have to declare it using "partial" generics:
Map<Object, List> map;
I know this is bad, but currently I have no better choice. Can generics be used in this situation?
UPDATE
Perhaps I did not clearly express my problem. I want a card that is capable of:
map.put(new Key<String>(), new ArrayList<String>()); map.put(new Key<Integer>(), new ArrayList<Integer>());
And the following code should not compile:
map.put(new Key<String>(), new ArrayList<Integer>());
The key and value must always have the same general type, while the general type can be any, and obviously the map extension does not meet my requirement.
java generics map
Zhao yi
source share