作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Java 8 流表达式,它有 3 个过滤器并且工作正常。
我想防止大多数值的过滤器中出现空指针异常。
这是表达式:
if(!purchasedTripSegments.isEmpty()) {
filteredList = purchasedTripSegments.stream()
.filter(segment -> PurchasedVendorType.RAIL.equals(segment.getVendorType()))
.filter(distinctByKeys(segment -> Arrays.asList(segment.getBillingMethod(),
segment.getOrigin().getNumberCode(), segment.getDestination().getNumberCode(),
segment.getStopOff().getStopOffLocation().getNumberCode())))
.filter(segment -> segment.getBillingMethod().equalsIgnoreCase(BILLING_METHOD_LOCAL) ||
(segment.getBillingMethod().equalsIgnoreCase(BILLING_METHOD_RULE) &&
segment.getDestination().getNumberCode() !=
segment.getStopOff().getStopOffLocation().getNumberCode()))
.collect(Collectors.toList());
}
所以 VendorType 不能为空。
.filter(Objects::nonNull)
我有一个测试用例,它有一个空目标对象,并且抛出了 NullPointerException。
Optional<List<PurchasedTripSegment>> filteredList = Optional.ofNullable(new ArrayList<>());
if(!purchasedTripSegments.isEmpty()) {
filteredList = purchasedTripSegments.stream()
.filter(segment -> PurchasedVendorType.RAIL.equals(segment.getVendorType()))
.filter(distinctByKeys(segment -> Arrays.asList(segment.getBillingMethod(),
segment.getOrigin().getNumberCode(),
segment.getDestination().getNumberCode(),
segment.getStopOff().getStopOffLocation().getNumberCode())))
.filter(segment -> BILLING_METHOD_LOCAL.equals(segment.getBillingMethod())
|| (BILLING_METHOD_RULE.equals(segment.getBillingMethod()) &&
segment.getDestination().getNumberCode() !=
segment.getStopOff().getStopOffLocation().getNumberCode()))
.collect(Collectors.toList());
}
我不确定如何将您建议的更改应用于我的过滤器。我尝试按书面方式添加,但无法识别 map()。
private Optional<Integer> getDestinationCode(PurchasedCostTripSegment purchasedCostTripSegment) {
return Optional.ofNullable(purchasedCostTripSegment.getDestination()) // empty for 'null'
.map(Destination::getNumberCode);
}
我对传入参数进行空检查。
getNumberCode
不被认可。
最佳答案
billingMethod
等属性, 只要有可能 null
内List
,它应该仍然适用于比较以获得不同的值。
另一方面,将它们与其他一些字符串常量进行比较可以通过用户 FilipRistic suggested 的方式解决。 .
但是,当涉及可能为空的对象并且您想安全地进一步访问内部属性时,您可以使用 Optional
并链接访问器。对于其中的示例,当您想访问 numberCode
时您的目的地可能为空,您可以在 PurchasedTripSegment
中有一个访问器类来公开这个:
Optional<Integer> getDestinationCode() {
return Optional.ofNullable(this.getDestination()) // empty for 'null'
.map(Node::getNumberCode);
}
对其他访问器进行类似的更改,您的整体代码将更新并更改为以下内容:
filteredList = purchasedTripSegments.stream()
.filter(segment -> PurchasedVendorType.RAIL.equals(segment.getVendorType()))
.filter(distinctByKey(segment -> Arrays.asList(segment.getBillingMethod(),
segment.getOriginCode(), segment.getDestinationCode(),
segment.getStopOffLocationCode())))
.filter(segment -> segment.getBillingMethod().equalsIgnoreCase(BILLING_METHOD_LOCAL) ||
(segment.getBillingMethod().equalsIgnoreCase(BILLING_METHOD_RULE) &&
segment.getDestinationCode().equals(segment.getStopOffLocationCode())))
.collect(Collectors.toList());
关于java - 过滤掉Java 8流过滤器表达式中的空值,因此不会抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62584902/
我正在编写一个快速的 preg_replace 来从 CSS 中删除注释。 CSS 注释通常有这样的语法: /* Development Classes*/ /* Un-comment me for
使用 MySQL,我有三个表: 项目: ID name 1 "birthday party" 2 "soccer match" 3 "wine tasting evening" 4
我是一名优秀的程序员,十分优秀!