如何在Java中将可选映射转换为流映射
我现在的逻辑是:如何在Java中将可选映射转换为流映射,java,java-stream,optional,Java,Java Stream,Optional,我现在的逻辑是: List<String> priceUnitCodes = ofNullable(product.getProductPrices()) .map(ProductPrices::getProductPrices) .flatMap(productPrices -> productPrices.stream()) // << error highlight .map(Pr
List<String> priceUnitCodes = ofNullable(product.getProductPrices())
.map(ProductPrices::getProductPrices)
.flatMap(productPrices -> productPrices.stream()) // << error highlight
.map(ProductPrice::getPriceBase)
.map(PriceBase::getPriceUnit)
.map(UniversalType::getCode)
.collect(Collectors.toList());
我知道
Optionals
和Stream
是两种不同的东西,但我想知道是否有一种方法可以将它们组合起来,这样我就可以继续执行可选,因为您是从可选
开始的,所以当可选
为空时,您必须决定返回什么
一种方法是将流
管道放入可选
的映射
:
List<String> priceUnitCodes = ofNullable(product.getProductPrices())
.map(ProductPrices::getProductPrices)
.map(productPrices -> productPrices.stream()
.map(ProductPrice::getPriceBase)
.map(PriceBase::getPriceUnit)
.map(UniversalType::getCode)
.collect(Collectors.toList())
.orElse(null);
List<String> priceUnitCodes = ofNullable(product.getProductPrices())
.map(ProductPrices::getProductPrices)
.map(productPrices -> productPrices.stream()
.map(pp -> pp.getPriceBase().getPriceUnit().getCode())
.collect(Collectors.toList())
.orElse(null);
ofNullable(product.getProductPrices())
.map(ProductPrices::getProductPrices)
.stream()
.flatMap(Collection::stream) //assuming getProductPrices returns a Collection
...
如果您使用的是Java 9+,则可以使用Optional.stream
,后跟flatMap
:
List<String> priceUnitCodes = ofNullable(product.getProductPrices())
.map(ProductPrices::getProductPrices)
.map(productPrices -> productPrices.stream()
.map(ProductPrice::getPriceBase)
.map(PriceBase::getPriceUnit)
.map(UniversalType::getCode)
.collect(Collectors.toList())
.orElse(null);
List<String> priceUnitCodes = ofNullable(product.getProductPrices())
.map(ProductPrices::getProductPrices)
.map(productPrices -> productPrices.stream()
.map(pp -> pp.getPriceBase().getPriceUnit().getCode())
.collect(Collectors.toList())
.orElse(null);
ofNullable(product.getProductPrices())
.map(ProductPrices::getProductPrices)
.stream()
.flatMap(Collection::stream) //assuming getProductPrices returns a Collection
...
可选。如果可选项为空,则流
返回空流。另一种解决方案是使用orElse
获取可选项
的值,这可以在不升级到Java-9的情况下完成。它看起来像:
List<String> priceUnitCodes = Optional.ofNullable(product.getProductPrices())
.map(ProductPrices::getProductPrices)
.orElse(Collections.emptyList()) // get the value from Optional
.stream()
.map(ProductPrice::getPriceBase)
.map(PriceBase::getPriceUnit)
.map(UniversalType::getCode)
.collect(Collectors.toList());
List priceUnitCodes=可选。不可用(product.getProductPrices())
.map(ProductPrices::getProductPrices)
.orElse(Collections.emptyList())//从可选的
.stream()
.map(ProductPrice::getPriceBase)
.map(PriceBase::getPriceUnit)
.map(UniversalType::getCode)
.collect(Collectors.toList());
什么是可用的
?我们看不到你的进口货,是这样还是?--什么是ProductPrices::getProductPrices
的返回类型?它的可选。对于nullable
您有一个类ProductPrices
,它有一个方法getProductPrices
,因此要获取产品价格,您必须调用product.getProductPrices().getProductPrices().getProductPrices())
?我不明白单一地图与多链地图之间的区别。不太清楚您是否试图说NPE处理在这种情况下是必须的。@Naman我的意思是,如果流管道中调用的任何方法都可能返回null值,那么我的代码(两个片段)就不够(您必须过滤掉null值,或者将流的元素包装为可选)。如果不可能使用null值,则两个代码段都是正确的,但我更喜欢单一的map
版本。好的,对我来说,“另一方面,如果它们永远不能返回null,那么它们可以链接到一个map”,听起来就像如果它们永远不能返回null,那么就可以链接到一个map。另一方面,我相信,基于消费者对方法参考与lambda表示的可读性感知,它们无论如何都可以链接到单个映射中。在功能上,他们会抛出NullPointerException
,用于失败案例。