问题描述
使用Java 8,我有这样的代码:
With Java 8, I have this code:
if(element.exist()){
// Do something
}
我想转换为lambda风格,
I want to convert to lambda style,
element.ifExist(el -> {
// Do something
});
使用 ifExist
方法,如下所示:
public void ifExist(Consumer<Element> consumer) {
if (exist()) {
consumer.accept(this);
}
}
但现在我还有其他情况要打电话:
But now I have else cases to call:
element.ifExist(el -> {
// Do something
}).ifNotExist(el -> {
// Do something
});
我可以写一个类似的 ifNotExist
,以及我希望它们是互斥的(如果存在
条件为真,则无需检查 ifNotExist
,因为有时候,exists()方法需要花费很多工作量来检查),但我总是要检查两次。我怎么能避免这种情况?
I can write a similar ifNotExist
, and I want they are mutually exclusive (if the exist
condition is true, there is no need to check ifNotExist
, because sometimes, the exist() method takes so much workload to check), but I always have to check two times. How can I avoid that?
也许存在这个词会让别人误解我的想法。您可以想象我还需要一些方法:
Maybe the "exist" word make someone misunderstand my idea. You can imagine that I also need some methods:
ifVisible()
ifEmpty()
ifHasAttribute()
许多人说这是个坏主意,但是:
Many people said that this is bad idea, but:
在Java 8中,我们可以使用lambda forEach而不是传统的来实现
循环。在编程中
和如果
是两个基本流控制。如果我们可以将lambda用于 for
循环,为什么使用lambda作为如果
错误的想法?
In Java 8 we can use lambda forEach instead of a traditional for
loop. In programming for
and if
are two basic flow controls. If we can use lambda for a for
loop, why is using lambda for if
bad idea?
for (Element element : list) {
element.doSomething();
}
list.forEach(Element::doSomething);
在Java 8中,带有ifPresent的可选
,类似于我对ifExist的看法:
In Java 8, there's Optional
with ifPresent, similar to my idea of ifExist:
Optional<Elem> element = ...
element.ifPresent(el -> System.out.println("Present " + el);
关于代码维护和可读性,如果我有以下代码,并且有许多重复简单的 if
子句,你怎么看?
And about code maintenance and readability, what do you think if I have the following code with many repeating simple if
clauses?
if (e0.exist()) {
e0.actionA();
} else {
e0.actionB();
}
if (e1.exist()) {
e0.actionC();
}
if (e2.exist()) {
e2.actionD();
}
if (e3.exist()) {
e3.actionB();
}
比较:
e0.ifExist(Element::actionA).ifNotExist(Element::actionB);
e1.ifExist(Element::actionC);
e2.ifExist(Element::actionD);
e3.ifExist(Element::actionB);
哪个更好?而且,oops,你注意到在传统的 if
子句代码中,有没有istake in:
Which is better? And, oops, do you notice that in the traditional if
clause code, there's a mistake in:
if (e1.exist()) {
e0.actionC(); // Actually e1
}
我想如果我们使用lambda,我们可以避免这个错误!
I think if we use lambda, we can avoid this mistake!
推荐答案
因为它几乎与可选不匹配,也许您可能会重新考虑逻辑:
As it almost but not really matches Optional, maybe you might reconsider the logic:
Java 8的表现力有限:
Java 8 has a limited expressiveness:
Optional<Elem> element = ...
element.ifPresent(el -> System.out.println("Present " + el);
System.out.println(element.orElse(DEFAULT_ELEM));
此处地图
可能会限制元素上的视图:
Here the map
might restrict the view on the element:
element.map(el -> el.mySpecialView()).ifPresent(System.out::println);
Java 9:
element.ifPresentOrElse(el -> System.out.println("Present " + el,
() -> System.out.println("Not present"));
一般来说,两个分支是不对称的。
In general the two branches are asymmetric.
这篇关于使用Java lambda而不是'if else'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!