所需类型:T提供:列表<;T>;在泛型Java类中

所需类型:T提供:列表<;T>;在泛型Java类中,java,list,generics,Java,List,Generics,因此,我在理解泛型的以下问题时遇到了问题 这是我班上的一个小小的平静: public class BaseFiller <T> { private final List<T> act_Bwa; public <T extends List<T>> Map<Integer, String> makeMapFromList() { List<T> res = new ArrayList<>();

因此,我在理解泛型的以下问题时遇到了问题

这是我班上的一个小小的平静:

public class BaseFiller <T> {

private final List<T> act_Bwa;

public <T extends List<T>> Map<Integer, String> makeMapFromList() {
    List<T> res = new ArrayList<>();

    for (List<T> list : act_Bwa) {
        res.addAll(list);
    }

    //removes all duplicates from List by DbNr
    List<T> resWithoutDuplicates = res.stream()
            .filter(distinctByProperty(intProperty))
            .collect(Collectors.toList());

    //creates HashMap (Key = DbNr, Value = name)
    return mapListToHashMap(resWithoutDuplicates, intProperty, stringProperty);
}
公共类基址填充程序{
私人最终名单法;
公共地图makeMapFromList(){
List res=new ArrayList();
用于(列表:act_Bwa){
res.addAll(列表);
}
//按DbNr从列表中删除所有重复项
List resWithoutDuplicates=res.stream()
.filter(distinctByProperty(intProperty))
.collect(Collectors.toList());
//创建HashMap(Key=DbNr,Value=name)
返回mapListToHashMap(resWithoutDuplicates、intProperty、stringProperty);
}
}

我收到以下错误消息:

public class BaseFiller <T> {

private final List<T> act_Bwa;

public <T extends List<T>> Map<Integer, String> makeMapFromList() {
    List<T> res = new ArrayList<>();

    for (List<T> list : act_Bwa) {
        res.addAll(list);
    }

    //removes all duplicates from List by DbNr
    List<T> resWithoutDuplicates = res.stream()
            .filter(distinctByProperty(intProperty))
            .collect(Collectors.toList());

    //creates HashMap (Key = DbNr, Value = name)
    return mapListToHashMap(resWithoutDuplicates, intProperty, stringProperty);
}
所需类型:T

提供:清单

分别为: 错误:(81,37)java:不兼容的类型:T无法转换为java.util.List

错误发生在for循环中

那么在这种情况下我做错了什么?有人能告诉我我做错了什么吗?

你可以这样做:

public <R extends List<T>> Map<Integer, String> makeMapFromList() {
    List<R> res = new ArrayList<>();

    for (List<R> list : act_Bwa) {
        res.addAll(list);
    }

    //removes all duplicates from List by DbNr
    List<R> resWithoutDuplicates = res.stream()
            .filter(distinctByProperty(intProperty))
            .collect(Collectors.toList());

    //creates HashMap (Key = DbNr, Value = name)
    return mapListToHashMap(resWithoutDuplicates, intProperty, stringProperty);
}
publicmap makeMapFromList(){
List res=new ArrayList();
用于(列表:act_Bwa){
res.addAll(列表);
}
//按DbNr从列表中删除所有重复项
List resWithoutDuplicates=res.stream()
.filter(distinctByProperty(intProperty))
.collect(Collectors.toList());
//创建HashMap(Key=DbNr,Value=name)
返回mapListToHashMap(resWithoutDuplicates、intProperty、stringProperty);
}

如果你想在课堂上使用
T
,也可以删除

for(List List:act\u Bwa)
-act\u Bwa不是一个列表列表。它应该是(T list:act_Bwa),或者您需要更改
act_Bwa的类型。您可能只需要
res.addAll(act_Bwa)
,而不需要for循环。您在一个名为
T
的类中的方法上有一个名为
T
的类型参数。虽然不是非法的,但它确实令人困惑,可以给其中一个命名为其他名称(或者去掉一个,我怀疑这是可能的)。你能告诉我这意味着什么吗:集合“res”的内容被查询,但从未更新这是怎么回事
act_Bwa
是一个
列表,而不是
列表的列表,更不用说
列表的未知子类型(
R
)。(列表:act_Bwa)
的循环根本不起作用。事实上,向该方法添加类型参数没有任何意义。正如我所理解的,我们可以从该方法中删除参数。。谢谢:)