简体   繁体   English

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

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

The question title may seem to be same as some other post but the content is different. 问题标题似乎与其他帖子相同,但内容不同。 So please don't mark it duplicate. 因此,请不要将其标记为重复。

Problem : 问题

I have the below class: 我有以下课程:

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

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

    //Getter setter

}

I am trying to using the below lambda expression to set the sCPairs . 我正在尝试使用下面的lambda表达式来设置sCPairs

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

But I have an compilation error saying: 但是我有一个编译错误说:

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

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

Can anyone please explain why this is happening and how to solve it? 谁能解释为什么会这样以及如何解决呢?

Thanks. 谢谢。

Your stream pipeline finds all the Map entries whose value List<String> contains the String "abc", and tries to collect them into a List<String> . 您的流管道会找到其值List<String>包含String “ abc”的所有Map条目,并尝试将它们收集到List<String>

You didn't specify how you intend to convert each Map.Entry<String,List<String>> element that passes the filter into a String . 你没有指定你打算如何各自将Map.Entry<String,List<String>>是通过过滤器进入一个元素String Depending on the required logic, perhaps you are missing a map() step after the filter. 根据所需的逻辑,也许您在过滤器之后缺少map()步骤。

For example, if you wish to collect all the keys having a value that passes the filter into a List<String> : 例如,如果您希望收集所有具有将过滤器传递到List<String>的值的键:

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

You are streaming entries from the map: 您正在从地图流式传输条目:

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

Then filtering out some of them: 然后过滤掉其中一些:

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

now you need to map the entries to List, for example: 现在您需要将条目映射到列表,例如:

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

and stream the contents of all these lists as one stream: 并将所有这些列表的内容作为一个流进行流式处理:

.flatMap(List::stream)

Finally collect the values into a list: 最后将值收集到一个列表中:

.collect(Collectors.toList());

You don't need to iterate over the entrySet since you are not using the key at all. 您根本不需要使用key,因此无需遍历entrySet You can get the values of the Map and filter that. 您可以获取Map的值并对其进行过滤。 Looks like this 看起来像这样

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

Since you want to convert a Map<String, List<String>> to a List<String> where the list should be a union of all matching value lists you'll need a flatMap() to join the streams on those value lists into a single stream. 由于您要将Map<String, List<String>>转换为List<String> ,其中列表应该是所有匹配值列表的并集,因此需要使用flatMap()将这些值列表上的流加入单个流。

Additionally you don't seem to need the keys so just stream on the map's values: 此外,您似乎不需要键,因此只需流式传输地图的值即可:

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.

相关问题 Java泛型不兼容的类型(不存在类型变量T的实例) - Java generics incompatible types (no instance(s) of type variable(s) T exist) Java错误:不兼容的类型:不存在类型变量R的实例,因此Stream <R> 符合布尔值 - Java Error: incompatible types: no instance(s) of type variable(s) R exist so that Stream<R> conforms to boolean Java 错误:不兼容的类型:不存在类型变量 T 的实例,因此可选<t>符合 Iterable<availablelicence></availablelicence></t> - Java error: incompatible types: no instance(s) of type variable(s) T exist so that Optional<T> conforms to Iterable<AvailableLicence> 不兼容的类型:不存在类型变量 T 的实例,因此 Matcher<t> 符合 ArgumentMatcher<long></long></t> - incompatible types: no instance(s) of type variable(s) T exist so that Matcher<T> conforms to ArgumentMatcher<Long> 不兼容的类型:不存在类型变量 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 Java泛型和不兼容的类型使用 - Java generics & incompatible types using <S extends {class}> 不兼容的类型泛型Java - Incompatible types generics Java Java泛型不兼容类型 - Java Generics Incompatible Types Java泛型:不兼容的类型 - Java generics: incompatible types Java通用问题:“类型不兼容;推断的类型参数java.lang.Object不符合类型变量T的界限 - Java generic issue: "incompatible types; inferred type argument(s) java.lang.Object do not conform to bounds of type variable(s) T
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM