Java 检查 Optionals 相等性的便捷方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37004579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convenient way of checking equality for Optionals
提问by Franz Ebner
I'm looking for a more convenient way of proofing equality for an Optional value.
我正在寻找一种更方便的方法来证明 Optional 值的相等性。
This is what an Oracle Blog postsuggests:
这是Oracle 博客文章的建议:
Optional<USB> maybeUSB = ...; maybeUSB.filter(usb -> "3.0".equals(usb.getVersion())
.ifPresent(() -> System.out.println("ok"));
IMHO results in something like
恕我直言,结果类似于
if (maybeUSB.filter(c -> "3.0".equals(c.getVersion())).isPresent()) {
...
}
Of course that's kind of a poor example because it compares the Version and not the instance of USB itself but I think it should still proof my point.
当然,这是一个糟糕的例子,因为它比较的是版本而不是 USB 本身的实例,但我认为它仍然应该证明我的观点。
Is this really as good as it gets?
这真的像它得到的那样好吗?
No
不
boolean presentAndEquals(Object)
or
或者
boolean deepEquals(Object)
Am I missing something here?
我在这里错过了什么吗?
EDIT:
编辑:
I'm not that happy with Optionals.equals either. Do I really have to box an Object first to instantly unbox and check for equality ?
我对 Optionals.equals 也不满意。我真的必须先装箱对象才能立即拆箱并检查相等性吗?
EDIT II:
编辑二:
Pretty damn happy with:
非常满意:
optional
.filter(nonOptional::equals)
.isPresent()
nowadays.
如今。
After some years of functional programming, if
looses a lotof relevance.
经过几年的函数式编程,if
失去了很多相关性。
采纳答案by slim
You have many options.
你有很多选择。
Already noted:
已经注意到:
boolean isEqual = maybeFoo.equals(Optional.of(testFoo));
Alternatively:
或者:
boolean isEqual = maybeFoo.isPresent() && maybeFoo.get().equals(testFoo);
Or:
或者:
boolean isEqual = testFoo.equals(maybeFoo.orElse(null));
These last two do have slightly different semantics: each returns a different value when maybeFoo
is empty and testFoo
is null. It's not clear which is the correct response (which I guess is one reason there's not a standard API method that does this).
最后两个确实有稍微不同的语义:当maybeFoo
为空和testFoo
为 null时,每个都返回不同的值。目前尚不清楚哪个是正确的响应(我想这是没有标准 API 方法执行此操作的原因之一)。
You can probably come up with others if you read the Optional
API doc and apply some thought. There's nothing magic that's absent from the docs.
如果您阅读Optional
API 文档并应用一些想法,您可能会想出其他人。文档中没有任何魔法。
More generally, if you're knocking against this often enough for it to bother you, you might be approaching Optional
with the wrong philosophy.
更一般地说,如果你经常反对它,以至于它打扰你,你可能会Optional
用错误的哲学来接近。
As I see it, Optional
is about acknowledging that something won't always be present, and that you need (sometimes verbose) code to handle that.
在我看来,Optional
是关于承认某些东西不会总是存在,并且您需要(有时是冗长的)代码来处理它。
This should be the exception. Wherever possible, try and create variables that can'tbe null or Optional.empty()
.
这应该是例外。在可能的情况下,尝试创建不能为 null 或Optional.empty()
.
In situations where this is unavoidable, embrace the fact that you need extra code.
在不可避免的情况下,接受您需要额外代码的事实。
回答by thecoop
Optional
implements the equals
method directly:
Optional
equals
直接实现方法:
if (maybeUSB.equals(Optional.ofNullable(testUSB))) {
...
}
(you can also use Objects.equals
rather than calling equals
directly)
(您也可以使用Objects.equals
而不是equals
直接调用)
EDIT:
编辑:
If you want both not present to be false, you can do this:
如果您不希望两者都不存在,则可以执行以下操作:
if (maybeUSB.equals(Optional.ofNullable(testUSB)) && maybeUSB.isPresent()) {
...
}
回答by Hai Phan
Would this work?
这行得通吗?
if (maybeUSB.map(c -> c.getVersion().equals("3.0")).orElse(false))