[英]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.