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

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

问题描述

即使我阅读了此链接 上的差异,但不清楚.那么谁能用代码简短地向我解释一下?

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

推荐答案

我想我开始理解你的问题了.Optional 的执行顺序可能与我们在过程编程中习惯的不同(Java 流和其他使用 lambda 的代码也是如此).

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 的输出中也看不到 Should I see this).orElseGet 再次查看 Optional 中是否存在值.如果存在,则返回该值并忽略我们传递的供应商.如果没有,则调用供应商,执行 { } 中的代码以获取要从 orElseGet() 返回的值.

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.在第二个 Supplier 被创建和浪费.在这两种情况下,您得到的回报都是简洁和空指针安全的代码.所以很多时候你选择哪一个并不重要.如果创建 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天全站免登陆