我不知道任何现有的库都可以精确地做到这一点,但是实现自己并不难。我过去做过几次类似的事情。您不能使用标准的Map接口,但是可以在内部使用哈希映射来实现您的类。首先,它可能看起来像这样:
public class KeyMap { public static class Key<T> { } private final HashMap<Object,List<?>> values = new HashMap<Object,List<?>>(); public <T> void put(Key<T> k, List<T> v) { values.put(k, v); } public <T> List<T> get(Key<T> k) { return (List<T>)values.get(k); } public static void main(String[] args) { KeyMap a = new KeyMap(); a.put(new Key<String>(), new ArrayList<String>()); a.get(new Key<Integer>()); }}


