确保Java中的通用通配符匹配

确保Java中的通用通配符匹配,java,generics,Java,Generics,因此,我的理解是: HashMap<Class<?>,List<?>> map map.put(ClassA.class, new Vector<ClassB>()); //I want this to refuse to compile 注意:我目前不在IDE前面,所以这是内存中的,但希望总体思路是清楚的。您可以定义泛型类型,比如在封闭方法的范围内,并将其用作泛型类和泛型向量的类型,或者更好的是,用作泛型集合 您可以通过以下方式调用它: me

因此,我的理解是:

HashMap<Class<?>,List<?>> map
map.put(ClassA.class, new Vector<ClassB>()); //I want this to refuse to compile

注意:我目前不在IDE前面,所以这是内存中的,但希望总体思路是清楚的。

您可以定义泛型类型,比如在封闭方法的范围内,并将其用作泛型类和泛型向量的类型,或者更好的是,用作泛型集合

您可以通过以下方式调用它:

method(String.class, new ArrayList<String>());
//or
method(String.class, new Vector<String>());
您可以注意到,此语句不会编译:

method(Integer.class, new Vector<String>());
myCollection.<String>put(String.class, new Vector<Integer>());
myCollection.put(String.class, new Vector<Integer>());

您可以在封闭方法的范围内定义泛型类型,并将其用作泛型类和泛型向量的类型,或者更好的是,用作泛型集合

您可以通过以下方式调用它:

method(String.class, new ArrayList<String>());
//or
method(String.class, new Vector<String>());
您可以注意到,此语句不会编译:

method(Integer.class, new Vector<String>());
myCollection.<String>put(String.class, new Vector<Integer>());
myCollection.put(String.class, new Vector<Integer>());

我不会直接使用HashMap,因为它的默认方法不允许实现您的期望。我将基于HashMap编写自己的集合,该集合将公开它自己的put方法。它可能会像这样:

public <T> void put(Class<T> clazz, List<T> list) {
    internalMap.put(clazz, list);
}
public <T, TCollection extends Collection<T>> TCollection get(Class<T> clazz) {
    return (TCollection) internalMap.get(clazz);
}
甚至像这样:

myCollection.<Integer>put(Integer.class, new Vector<Integer>());
myCollection.<String>put(String.class, new Vector<String>());
myCollection.put(Integer.class, new Vector<Integer>());
myCollection.put(String.class, new Vector<String>());
myCollection.<Integer>put(Integer.class, new Vector<Integer>());
myCollection.<String>put(String.class, new ArrayList<String>());
myCollection.put(Integer.class, new Vector<Integer>());
myCollection.put(String.class, new Vector<String>());
对于具有显式泛型参数的情况,用法将略有更新:

myCollection.<Integer, Vector<Integer>>put(Integer.class, new Vector<Integer>());
myCollection.<String, ArrayList<String>>put(String.class, new ArrayList<String>());
那么你有两个问题:

现在的呼吁需要一些必要的丑陋: 并将简单地这样使用:

myCollection.<Integer>put(Integer.class, new Vector<Integer>());
myCollection.<String>put(String.class, new Vector<String>());
myCollection.put(Integer.class, new Vector<Integer>());
myCollection.put(String.class, new Vector<String>());
myCollection.<Integer>put(Integer.class, new Vector<Integer>());
myCollection.<String>put(String.class, new ArrayList<String>());
myCollection.put(Integer.class, new Vector<Integer>());
myCollection.put(String.class, new Vector<String>());
或者像这样:

myCollection.<Integer>put(Integer.class, new Vector<Integer>());
myCollection.<String>put(String.class, new Vector<String>());
myCollection.put(Integer.class, new Vector<Integer>());
myCollection.put(String.class, new Vector<String>());
myCollection.<Integer>put(Integer.class, new Vector<Integer>());
myCollection.<String>put(String.class, new ArrayList<String>());
myCollection.put(Integer.class, new Vector<Integer>());
myCollection.put(String.class, new Vector<String>());

这与

相同,我不会直接使用HashMap,因为它的默认方法不允许实现您想要的。我将基于HashMap编写自己的集合,该集合将公开它自己的put方法。它可能会像这样:

public <T> void put(Class<T> clazz, List<T> list) {
    internalMap.put(clazz, list);
}
public <T, TCollection extends Collection<T>> TCollection get(Class<T> clazz) {
    return (TCollection) internalMap.get(clazz);
}
甚至像这样:

myCollection.<Integer>put(Integer.class, new Vector<Integer>());
myCollection.<String>put(String.class, new Vector<String>());
myCollection.put(Integer.class, new Vector<Integer>());
myCollection.put(String.class, new Vector<String>());
myCollection.<Integer>put(Integer.class, new Vector<Integer>());
myCollection.<String>put(String.class, new ArrayList<String>());
myCollection.put(Integer.class, new Vector<Integer>());
myCollection.put(String.class, new Vector<String>());
对于具有显式泛型参数的情况,用法将略有更新:

myCollection.<Integer, Vector<Integer>>put(Integer.class, new Vector<Integer>());
myCollection.<String, ArrayList<String>>put(String.class, new ArrayList<String>());
那么你有两个问题:

现在的呼吁需要一些必要的丑陋: 并将简单地这样使用:

myCollection.<Integer>put(Integer.class, new Vector<Integer>());
myCollection.<String>put(String.class, new Vector<String>());
myCollection.put(Integer.class, new Vector<Integer>());
myCollection.put(String.class, new Vector<String>());
myCollection.<Integer>put(Integer.class, new Vector<Integer>());
myCollection.<String>put(String.class, new ArrayList<String>());
myCollection.put(Integer.class, new Vector<Integer>());
myCollection.put(String.class, new Vector<String>());
或者像这样:

myCollection.<Integer>put(Integer.class, new Vector<Integer>());
myCollection.<String>put(String.class, new Vector<String>());
myCollection.put(Integer.class, new Vector<Integer>());
myCollection.put(String.class, new Vector<String>());
myCollection.<Integer>put(Integer.class, new Vector<Integer>());
myCollection.<String>put(String.class, new ArrayList<String>());
myCollection.put(Integer.class, new Vector<Integer>());
myCollection.put(String.class, new Vector<String>());

这与

在哪里声明和实例化映射相同?除了Object之外,是否有一个由潜在类实现的公共超类或接口?映射在哪里声明和实例化?除了Object之外,潜在类是否实现了一个公共的超类或接口?实际上,它应该是method;。我自己也经常对此感到困惑,因为我来自.NET背景。我的示例中甚至不需要这一部分。我编辑了它。谢谢:事实上,这将是方法;。我自己也经常对此感到困惑,因为我来自.NET背景。我的示例中甚至不需要这一部分。我编辑了它。谢谢:internalMap必须使用与方法签名中定义的类型相同的类型T进行参数化。由于internalMap是一个类成员,因此我将在类级别定义泛型T类型:@kocko,很好地指出,我忘了提到我的内部映射的类型现在是固定的。当然,它不会使用泛型T,因为它会被违反,因此它会使用?占位符。我如何使它更通用。e、 g.将列表替换为在类级别定义的泛型类型。类似于public void put Class clazz,T1 T1……如何确保T1可以处理泛型参数?@过锁,签出我的更新,我已经处理了您的评论。必须使用与方法签名中定义的类型相同的类型T对internalMap进行参数化。由于internalMap是一个类成员,因此我将在类级别定义泛型T类型:@kocko,很好地指出,我忘了提到我的内部映射的类型现在是固定的。当然,它不会使用泛型T,因为它会被违反,因此它会使用?占位符。我如何使它更通用。e、 g.将列表替换为在类级别定义的泛型类型。类似于公共void put类clazz,T1……如何确保T1可以处理泛型参数?@overclockwise,checkout my update,我已经回答了您的评论。