Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中将可选映射转换为流映射_Java_Java Stream_Optional - Fatal编程技术网

如何在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
,用于失败案例。