当我需要在Optional.orElse()上使用Optional.orElseGet()时 [英] When I need to use Optional.orElseGet() over Optional.orElse()

查看:196
本文介绍了当我需要在Optional.orElse()上使用Optional.orElseGet()时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要清楚解释这个,即使我读过这个链接关于差异但没有明确的清晰度。所以任何人都可以用代码向我解释这个问题。

I need clear explanation on this, even though I read this link on differences but no clear clarity. So anyone explain me on this in short with code.

推荐答案

我想我开始明白你的问题。带有可选的执行顺序可能与我们在程序编程中使用的顺序不同(Java流和使用lambdas的其他代码也是如此)。

I think I am starting to understand your question. Execution order with Optional can be different from what we are used to in procedural programming (the same is true for Java streams and other code using lambdas).

我将使用 Eugene的回答中的两个例子:

I will use the two examples from Eugene’s answer:

    o1.orElse(new MyObject()); // 1055e4af 

这是普通的旧Java:它是对 orElse的调用( ) new MyObject()作为参数。因此,首先评估参数,并创建一个新的 MyObject 。然后将其传递给 orElse() orElse()查看 Optional 中是否存在值;如果是这样,它返回该值(丢弃新创建的对象);如果没有,它返回参数中给定的对象。这是一个更简单的例子。

This is plain old Java: it’s a call to orElse() taking new MyObject() as argument. So the argument is evaluated first and a new MyObject created. This is then passed to orElse(). orElse() looks to see whether a value is present in the Optional; if so it returns that value (discarding the newly created object); if not, it returns the object given to it in the argument. This was the simpler example.

    o1.orElseGet(() -> {
        System.out.println("Should I see this");
        return new MyObject();
    });

我们再次使用一个参数进行方法调用,并再次首先计算参数。 lambda仅作为供应商创建和传递。 {} 中的代码尚未执行(你也看不到我应该在Eugene的输出中看到这个)。再次 orElseGet 查看 Optional 中是否存在值。如果有,则返回该值,并忽略我们传递的供应商。如果没有,则调用供应商,执行 {} 中的代码,以获取从或ElseGet()返回的值

Again we have a method call with one argument, and again the argument is evaluated first. The lambda is only created and passed as a supplier. The code inside { } is not executed yet (you also see no Should I see this in Eugene’s output). Again orElseGet looks to see if there is a value present in the Optional. If there is, the value is returned and the supplier we passed is ignored. If there isn’t, the supplier is invoked, the code inside { } is executed to get the value to be returned from orElseGet().

在第一种情况下,可以说创建了 MyObject 并浪费了。在第二个中,创建并浪费了供应商。你得到的回报是两种情况下的简洁和空指针安全代码。所以你选择哪一个并不重要。如果创建 MyObject 代价高昂或者有不必要的副作用,你当然会想要第二个版本,其中只有在被要求时才创建对象,并且永远不会浪费。注释中的Eugene提到了返回的对象来自数据库调用的情况。数据库调用通常非常耗时,以至于您不想出于任何目的。

In the first case, one may say that a MyObject is created and wasted. In the second a Supplier is created and wasted. What you get in return is terse and null-pointer safe code in both cases. So very often it’s not important which one you pick. If creating the MyObject is costly or has unwanted side effects, you will of course want the second version where the object is only created when it is asked for, and is never wasted. Eugene in a comment mentions the case where the returned object comes from a database call. Database calls are usually time-consuming enough that you don’t want to make one for no purpose.

这篇关于当我需要在Optional.orElse()上使用Optional.orElseGet()时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆