IfPresentOrElse场景在Java 8流中

huangapple go评论76阅读模式
英文:

IfPresentOrElse scenario in java 8 streams

问题

我有一个使用嵌套流的场景。以下是代码:

    list.parallelStream()
        .filter(item -> productList.parallelStream()
            .anyMatch(product -> product.getProductName().equals(item.getProductName())
                && item.getQuantity() <= product.getAvailableQuantity()));

在这里,我试图根据“productnames”进行过滤,这部分运行得很好,但我需要在anymatch中添加一个else条件。如果没有找到匹配项,我需要抛出一个错误“product not found”。我尝试使用ifPresentOrElse,但它需要一个Consumer接口作为参数,返回void(但在我的情况下必须返回一个布尔值)。任何帮助将不胜感激。

谢谢。

英文:

I have a scenario where I am using nested streams. PFB the code:

    list.parallelStream()
        .filter(item -&gt; productList.parallelStream()
    		.anyMatch(product -&gt; product.getProductName().equals(item.getProductName())
    			 &amp;&amp; item.getQuantity() &lt;= product.getAvailableQuantity()));

Here I am trying to filter out based on the productnames which is working perfectly fine, but I need to add an else condition to anymatch. If there is no match found, I need to throw an error "product not found". I tried to use ifPresentOrElse but it takes Consumer interface as an argument which returns void(but in my case it has to return a boolean value). Any help is appreciated.

Thanks.

答案1

得分: 2

You can use orElseThrow.

orElseThrow(ProductNotFoundException::new)

你可以使用 orElseThrow

orElseThrow(ProductNotFoundException::new)

You can't use orElseThrow() with anyMatch() as it returns boolean.
You can use findAny() on the filter() which will return Optional and then you can throw exception using orElseThrow() on Optional.

For example:

 list.parallelStream()
        .filter(item -> productList.parallelStream()
            .anyMatch(product -> product.getProductName().equals(item.getProductName())
                 && item.getQuantity() <= product.getAvailableQuantity()))
        .findAny().orElseThrow(ProductNotFoundException::new);

你不能在 anyMatch() 中使用 orElseThrow(),因为它返回布尔值。
你可以在 filter() 上使用 findAny(),它会返回 Optional,然后你可以在 Optional 上使用 orElseThrow() 来抛出异常。

例如:

 list.parallelStream()
        .filter(item -> productList.parallelStream()
            .anyMatch(product -> product.getProductName().equals(item.getProductName())
                 && item.getQuantity() <= product.getAvailableQuantity()))
        .findAny().orElseThrow(ProductNotFoundException::new);

Edit:

OP 希望在找不到第一个产品时抛出错误。

        list.parallelStream()
                .filter(item -> {
                    if (productList.parallelStream().anyMatch(product -> product.getProductName().equals(item.getProductName())
                                    && item.getQuantity() <= product.getAvailableQuantity())) {
                        return true;
                    } else {
                        throw new ProductNotFoundException();
                    }
                });

请为流添加终止操作以执行。

                });

请为流添加终止操作以执行。

英文:

You can use orElseThrow.

orElseThrow(ProductNotFoundException::new)

You can't use orElseThrow() with anyMatch() as it returns boolean.
You can use findAny() on the filter() which will return Optional and then you can throw exception using orElseThrow() on Optional.

For example :

 list.parallelStream()
        .filter(item -&gt; productList.parallelStream()
            .anyMatch(product -&gt; product.getProductName().equals(item.getProductName())
                 &amp;&amp; item.getQuantity() &lt;= product.getAvailableQuantity()))
        .findAny().orElseThrow(ProductNotFoundException::new);

Edit :

OP wants to throw error on first product that is not found.

        list.parallelStream()
                .filter(item -&gt; {
                    if (productList.parallelStream().anyMatch(product -&gt; product.getProductName().equals(item.getProductName())
                                    &amp;&amp; item.getQuantity() &lt;= product.getAvailableQuantity())) {
                        return true;
                    } else {
                        throw new ProductNotFoundException();
                    }
                });

Do add a terminal operation for the stream to execute.

huangapple
  • 本文由 发表于 2020年8月12日 14:31:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63371040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定