简体   繁体   English

java:不兼容的类型:推断变量 RR 的边界不兼容

[英]java: incompatible types: inference variable RR has incompatible bounds

jdk 1.8 JDK 1.8

Set and Collection.设置和收藏。

I want to get count of intersection that > 0.我想获得 > 0 的交点计数。

I try this:我试试这个:

    SortedSet<VsatVlan> vsatVlans;
  Collection<Integer> mobilityNsVlansList = mobilityNS.getVlanIds();
    ....
      long intersectionCount = vsatVlans.stream()
                        .filter(vsatVlan -> mobilityNsVlansList.contains(vsatVlan.getVlanId()))
                        .collect(Collectors.collectingAndThen(
                                Collectors.toSet(),
                                set -> { // error here
                                    if (set.size() != 1) {
                                        throw new IllegalStateException();
                                    }
                                    return set.size();
                                }
                        ));

But I get error:但我得到错误:

incompatible parameter types in lambda expression: expected Set<Object> but found Set<VsatVlan>

java: incompatible types: inference variable RR has incompatible bounds
    lower bounds: java.lang.Long,java.lang.Object
    lower bounds: java.lang.Integer

If I get it right you want to return count of intersection only if not zero else you want to throw an exception.如果我做对了,您只想在不为零的情况下返回交集计数,否则您想抛出异常。 You need to explicitly supply the type of the set to get rid of the error you get:您需要明确提供集合的类型以消除您得到的错误:

return vsatVlans.stream()
                .filter(vsatVlan -> mobilityNsVlansList.contains(vsatVlan.getVlanId()))
                .collect(Collectors.collectingAndThen(Collectors.toSet(),
                    (Set<VsatVlan> set) -> {
                        if (set.size() == 0) {
                            throw new IllegalStateException("");
                        }
                        return set.size();
                    }
               ));

You also could use Optionals here and and avoid the if block:您也可以在此处使用 Optionals 并避免使用 if 块:

return vsatVlans.stream()
               .filter(vsatVlan -> mobilityNsVlansList.contains(vsatVlan.getVlanId()))
               .collect(Collectors.collectingAndThen(Collectors.counting(), Optional::of))
               .filter(x -> x > 0)
               .orElseThrow(() -> new IllegalStateException(""));

but IMO counting and then checking in a separate if block, is way more readable than the above two:但是 IMO 计数然后检查一个单独的 if 块,比上面两个更易读:

long count = vsatVlans.stream()
                      .filter(vsatVlan -> mobilityNsVlansList.contains(vsatVlan.getVlanId()))
                      .count();
if(count == 0){
    new IllegalStateException("")
}
return count;

暂无
暂无

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

相关问题 java EnumSet,不兼容的类型:推断变量E具有不兼容的范围 - java EnumSet, incompatible types: inference variable E has incompatible bounds 错误:不兼容的类型:推断变量R具有不兼容的范围 - error: incompatible types: inference variable R has incompatible bounds 错误:不兼容的类型:推断变量R具有不兼容的界限(Lambda Java 8) - error: incompatible types: inference variable R has incompatible bounds (Lambda java 8) Java “错误:不兼容的类型:推理变量 E 的边界不兼容”错误 - Java "error: incompatible types: inference variable E has incompatible bounds" error java:不兼容的类型:推理变量 T 具有不兼容的边界等式约束:下限:java.util.List&lt;&gt; 用于添加字段 - java: incompatible types: inference variable T has incompatible bounds equality constraints: lower bounds: java.util.List<> for adding field java:不兼容类型:推理变量T具有不兼容的边界等式约束:下限:java.util.List &lt;&gt; - java: incompatible types: inference variable T has incompatible bounds equality constraints: lower bounds: java.util.List<> 不兼容的类型:推理变量T具有不兼容的边界等式约束:捕获#1的? 扩展java.lang.Object - incompatible types: inference variable T has incompatible bounds equality constraints: capture#1 of ? extends java.lang.Object Java实体组件系统-推断变量T具有不兼容的范围 - Java Entity Component System - Inference variable T has incompatible bounds Java 8编译错误“推理变量D具有不兼容的范围” - Java 8 compilation error “inference variable D has incompatible bounds” 推理变量具有不兼容的边界。 Java 8编译器回归? - Inference variable has incompatible bounds. Java 8 Compiler Regression?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM