Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java泛型不兼容类型(不存在类型变量的实例)_Java_Generics_Lambda_Java 8 - Fatal编程技术网

Java泛型不兼容类型(不存在类型变量的实例)

Java泛型不兼容类型(不存在类型变量的实例),java,generics,lambda,java-8,Java,Generics,Lambda,Java 8,问题标题可能与其他帖子的标题相同,但内容不同。所以请不要将其标记为重复 问题: 我有以下课程: public class SCDTO extends RDTO { private List<String> sCPairs = Collections.emptyList(); public SCDTO(List<String> sCPairs) { this.sCPairs = sCPairs; } //Getter se

问题标题可能与其他帖子的标题相同,但内容不同。所以请不要将其标记为重复

问题

我有以下课程:

public class SCDTO extends RDTO {
    private List<String> sCPairs = Collections.emptyList();

    public SCDTO(List<String> sCPairs) {
        this.sCPairs = sCPairs;
    }

    //Getter setter

}
但我有一个编译错误说:

no instance(s) of type variable(s) exist so that Entry<String, List<String>> conforms to String
不存在类型变量的实例,因此条目符合字符串
util.getSCMap
返回
Map

谁能解释一下为什么会发生这种情况以及如何解决


谢谢。

您的流管道会找到其值
列表
包含
字符串
“abc”的所有
映射
条目,并尝试将它们收集到
列表

您没有指定要如何转换每个
映射。将筛选器传递到
字符串的
元素。根据所需的逻辑,您可能缺少过滤器后面的
map()
步骤

例如,如果您希望收集具有值的所有键,该值将过滤器传递到
列表中

sCPairsObject.setSCPairs(util.getSCMap()
.entrySet()
.stream()
.filter(条目->条目.getValue().contains(“abc”))
.map(map.Entry::getKey)
.collect(收集器.toCollection(ArrayList::new));

您正在从地图中对条目进行流式处理:

sCPairsObject.setSCPairs(util.getSCMap().entrySet().stream()
然后过滤掉其中一些:

.filter(entry -> entry.getValue().contains("abc"))
现在您需要将条目映射到列表,例如:

.map(entry -> entry.getValue())
并将所有这些列表的内容作为一个流:

.flatMap(List::stream)
最后,将值收集到一个列表中:

.collect(Collectors.toList());

您不需要迭代
入口集
,因为您根本不使用键。您可以获取
映射的值
并对其进行过滤。看起来像这样

sCPairsObject.setSCPairs(util.getSCMap()
   .values()
   .stream()
   .filter(entry -> entry.contains("abc"))
   .flatMap(List::stream)
   .collect(Collectors.toList());

由于要将
映射
转换为
列表
,其中列表应为所有匹配值列表的并集,因此需要
flatMap()
将这些值列表上的流合并为单个流

此外,您似乎不需要这些键,因此只需在地图的值上进行流式处理即可:

List<String> scPairs = util.getSCMap().values().stream() //stream on the values only
                           .filter( l -> l.contains( "abc" ) ) //filter the lists
                           .flatMap( Collection::stream ) //join the individual streams
                           .collect( Collectors.toList() ); //collect the result into a single list
List scPairs=util.getSCMap().values().stream()//仅对值执行流操作
.filter(l->l.contains(“abc”)//过滤列表
.flatMap(Collection::stream)//加入各个流
.collect(Collectors.toList())//将结果收集到单个列表中

entrySet()
将返回一个
集合
,因此如果您想将这些集合收集到
列表
中,您需要一个
映射(…)
介于两者之间,或者如果
V
实际上是一个
列表
您甚至可能需要一个
平面映射(…)
。非常感谢。现在我明白了:)非常感谢。。我不能把所有的答案都标记为答案,否则我也会把这个标记为答案。我想我是第一个有完整答案的答案。但是IMO Murat的答案是目前最好的,因为它考虑到您没有使用密钥,所以您不需要迭代条目。当前被接受的答案实际上并不能解决您的问题,因为它并没有调整值。请您解释一下需要做什么。flatMap(List::stream)?如果我不做,会发生什么@Adrian Kosterflatmap允许您将列表流的内容收集到单个流中。如果不使用它,则流将包含多个列表。然后,如果您确实收集(Collectors.toList()),您将得到一个列表而不是列表。您可以将变量“entry”重命名为例如“value”,以避免混淆
sCPairsObject.setSCPairs(util.getSCMap()
   .values()
   .stream()
   .filter(entry -> entry.contains("abc"))
   .flatMap(List::stream)
   .collect(Collectors.toList());
List<String> scPairs = util.getSCMap().values().stream() //stream on the values only
                           .filter( l -> l.contains( "abc" ) ) //filter the lists
                           .flatMap( Collection::stream ) //join the individual streams
                           .collect( Collectors.toList() ); //collect the result into a single list