繁体   English   中英

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

[英]Java generics incompatible types (no instance(s) of type variable(s) exist)

问题标题似乎与其他帖子相同,但内容不同。 因此,请不要将其标记为重复。

问题

我有以下课程:

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

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

    //Getter setter

}

我正在尝试使用下面的lambda表达式来设置sCPairs

sCPairsObject.setSCPairs(util.getSCMap().entrySet().stream().
                        filter(entry -> entry.getValue().contains("abc")).collect(Collectors.toCollection(ArrayList<String>::new)));

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

no instance(s) of type variable(s) exist so that Entry<String, List<String>> conforms to String

util.getSCMap返回Map<String, List<String>>

谁能解释为什么会这样以及如何解决呢?

谢谢。

您的流管道会找到其值List<String>包含String “ abc”的所有Map条目,并尝试将它们收集到List<String>

你没有指定你打算如何各自将Map.Entry<String,List<String>>是通过过滤器进入一个元素String 根据所需的逻辑,也许您在过滤器之后缺少map()步骤。

例如,如果您希望收集所有具有将过滤器传递到List<String>的值的键:

 sCPairsObject.setSCPairs(util.getSCMap()
                              .entrySet()
                              .stream()
                              .filter(entry -> entry.getValue().contains("abc"))
                              .map(Map.Entry::getKey)
                              .collect(Collectors.toCollection(ArrayList<String>::new)));

您正在从地图流式传输条目:

sCPairsObject.setSCPairs(util.getSCMap().entrySet().stream()

然后过滤掉其中一些:

.filter(entry -> entry.getValue().contains("abc"))

现在您需要将条目映射到列表,例如:

.map(entry -> entry.getValue())

并将所有这些列表的内容作为一个流进行流式处理:

.flatMap(List::stream)

最后将值收集到一个列表中:

.collect(Collectors.toList());

您根本不需要使用key,因此无需遍历entrySet 您可以获取Map的值并对其进行过滤。 看起来像这样

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

由于您要将Map<String, List<String>>转换为List<String> ,其中列表应该是所有匹配值列表的并集,因此需要使用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

不兼容的类型:不存在类型变量 F、T 的实例,因此 java.util.Collection<t> 符合 java.util.Set <java.lang.long< div><div id="text_translate"><p> 我正在尝试将ComplexItem列表转换为它们对应的 ID Long列表。 但是即使在使用(Collection&lt;ComplexItem&gt;)对getCollection()调用进行类型转换后,也不会出现上述错误 go</p><pre> Set&lt;Long&gt; ids = Collections2.transform( getComplexItems(), new Function&lt;ComplexItem, Long&gt;() { @Override public Long apply(final ComplexItem item) { return item.id; } })); public List&lt;ComplexItem&gt; getComplexItems() {.............. }</pre> </div></java.lang.long<></t>

[英]incompatible types: no instance(s) of type variable(s) F,T exist so that java.util.Collection<T> conforms to java.util.Set<java.lang.Long

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM