简体   繁体   English

不兼容的类型:不存在类型变量 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

I am trying to convert the list of ComplexItem to a list of their corresponding IDs Long .我正在尝试将ComplexItem列表转换为它们对应的 ID Long列表。 But getting the above error which doesn't go even after typecasting the getCollection() call with (Collection<ComplexItem>)但是即使在使用(Collection<ComplexItem>)getCollection()调用进行类型转换后,也不会出现上述错误 go

Set<Long> ids = Collections2.transform(
                    getComplexItems(), new Function<ComplexItem, Long>() {
                        @Override public Long apply(final ComplexItem item) {
                            return item.id;
                        }
                    }));

 public List<ComplexItem> getComplexItems() {
        ..............
 }

There's no reason to expect that the result of Collections2.transform , which is a Collection , will be magically transformed to a Set .没有理由期望 Collections2.transform 的Collections2.transform是一个Collection ,会神奇地转换为一个Set This is the reason for the type matching error in the title.这就是标题中类型匹配错误的原因。 You'll need either to convert the result to a set explicitly or make do with the Collection .您需要将结果显式转换为集合或使用Collection

Since you're already using Guava , you should strongly consider ImmutableSet , so it's由于您已经在使用Guava ,因此您应该强烈考虑ImmutableSet ,所以它是

ImmutableSet<Long> ids 
    = ImmutableSet.copyOf(Collections2.transform(getComplexItems(), item -> item.id)));

Taking a step back, remember Guava was created before Java Stream s.退一步,记住 Guava 是在 Java Stream之前创建的。 It's generally preferable to use language built-ins rather than a third party library, even when it's as good as Guava.通常最好使用内置语言而不是第三方库,即使它和 Guava 一样好。 In other words, prefer换句话说,更喜欢

getComplextItems().stream().map(item -> item.id).collect(toImmutableSet());

where toImmutableSet() is a collector defined by ImmutableSet .其中toImmutableSet()是由ImmutableSet定义的收集器。

you got in the wrong import for Function你输入了错误的 Function

try this尝试这个

    Collection<Long> ids = Collections2.transform(
        getComplexItems(), new com.google.common.base.Function<ComplexItem, Long>() {
            @Override public Long apply(final ComplexItem item) {
                return item.id;
            }
        });

暂无
暂无

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

相关问题 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> 无法从类型 [java.util.LinkedHashSet<!--?--> ] 输入 [java.util.Set<java.lang.long> ]</java.lang.long> - Failed to convert from type [java.util.LinkedHashSet<?>] to type [java.util.Set<java.lang.Long>] 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的实例) - Java generics incompatible types (no instance(s) of type variable(s) T exist) java.util.Collection的经典集合操作 - Classical set operations for java.util.Collection 不存在类型变量的实例,因此T符合注释 - No instance(s) of type variable(s) exist so that T conforms to Annotation Java泛型不兼容类型(不存在类型变量的实例) - Java generics incompatible types (no instance(s) of type variable(s) exist) java.util.Set的等效模式数据类型是什么? - What's the equivalent schema datatype for java.util.Set? 为什么java.util.Set无法返回任何值? - why java.util.Set can't return any value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM