java中泛型方法的推断

java中泛型方法的推断,java,generics,Java,Generics,我得到了以下场景:我有classclass>acl和collectioncollection>entitiesCollection并试图将它们传递给方法void doIndex(class clz,Iterable items),但不断得到编译时异常。我尝试了通配符捕获和其他方法,但没有成功。我一定错过了什么明显的东西。如有任何建议,我将不胜感激 Multimap<Class<? extends IModel<?>>, IModel<?>> ent

我得到了以下场景:我有class
class>acl
和collection
collection>entitiesCollection
并试图将它们传递给方法
void doIndex(class clz,Iterable items)
,但不断得到编译时异常。我尝试了通配符捕获和其他方法,但没有成功。我一定错过了什么明显的东西。如有任何建议,我将不胜感激

Multimap<Class<? extends IModel<?>>, IModel<?>> entityMultimap = ArrayListMultimap.create();

for (IModel<?> entity  : models) {

      Class<? extends IModel<?>> aCls = entity.getClass();
       entityMultimap.put(aCls, entity);          
    }



for (Class<? extends HasKey<?>> cls : entityMultimap.keySet()) {
   Collection<? extends IModel<?>> entitiesCollection = entityMultimap.get(cls);
        doIndex(cls, entitiesCollection);
}


public static <T extends IModel<T>> void doIndex(Class<T> clz, Iterable<T> items) {

    .....
}

它们与
clz
项的关系类似,但我不知道如何将它们传递到
doIndex

如果你已经发布了所有的来源,我可以自己尝试一下。但现在,我只能猜一猜。尝试:

public static <T extends IModel<?>> void doIndex(Class<T> clz, Iterable<T> items)
public staticA
Foo>
不能被视为
Foo
。不一样。两个
s中的每一个都是独立的,可能指不同的未知事物,但两个
T
s都指相同的未知事物

如果
doIndex
有两个类型参数
T
C
,形式为:
,则可以使用
参数或
Iterable
参数调用它,但不能同时调用这两个参数。因此,这将编译:

static <T,C extends IModel<T>> void doIndex(Class<C> clz, Iterable<C> items) {}
static {
    Class<? extends IModel<?>> cls = null;
    Collection<? extends IModel<?>> entitiesCollection = null;
    doIndex(cls, null);
    doIndex(null, entitiesCollection);
}
为什么??与以前相同的问题:
cls
s不一定与
实体集合的
相同

我不知道是否有好的解决办法。如果在调用方法上也使用类似于
的类型参数,然后在整个代码中使用
T
而不是问号,则可以使对
doIndex
的调用起作用

如果真的不能这样做(有时是这样),并且必须在所有地方都使用问号,那么我觉得类型参数没有用,应该删除或绕过。您可能知道这一点,但即使使用原始方法声明,删除泛型的不安全强制转换也可以完成任务:

static <T extends IModel<T>> void doIndex(Class<T> clz, Iterable<T> items) {}
static {
    Class<? extends IModel<?>> cls = null;
    Collection<? extends IModel<?>> entitiesCollection = null;
    doIndex((Class)cls, (Collection)entitiesCollection); // has a warning, but compiles
}
优点也是缺点:
doIndex
不能再要求
类和
Iterable
彼此兼容。(但是,如果iterable不是空的,则该方法可以执行运行时检查,以查看其中的对象是否与class参数兼容。)


基本问题是,
doIndex
要求对其参数的参数类型进行约束,您没有要实现的信息,因为您的所有其他类型参数都是问号。

您的编译错误是什么?这就是我甚至无法编译它的问题。错误:)java:类A中的doIndexFor方法无法应用于给定类型;必需:java.lang.Class,java.lang.Iterable已找到:java.lang.Class>原因:不存在类型变量的实例,因此参数类型为java.util.Collection
static <T,C extends IModel<T>> void doIndex(Class<C> clz, Iterable<C> items) {}
static {
    Class<? extends IModel<?>> cls = null;
    Collection<? extends IModel<?>> entitiesCollection = null;
    doIndex(cls, null);
    doIndex(null, entitiesCollection);
}
    doIndex(cls, entitiesCollection);
static <T extends IModel<T>> void doIndex(Class<T> clz, Iterable<T> items) {}
static {
    Class<? extends IModel<?>> cls = null;
    Collection<? extends IModel<?>> entitiesCollection = null;
    doIndex((Class)cls, (Collection)entitiesCollection); // has a warning, but compiles
}
static void doIndex(Class<? extends IModel<?>> clz, Iterable<? extends IModel<?>> items) {}