使用 Java lambda 而不是“if else" [英] Use Java lambda instead of 'if else'

查看:29
本文介绍了使用 Java lambda 而不是“if else"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 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,而且我希望它们是互斥的(如果exist条件为真,则不需要检查ifNotExist,因为有时,exist() 方法需要很多工作量来检查),但我总是要检查两次.我怎样才能避免这种情况?

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 代替传统的 for 循环.在编程中forif 是两个基本的流程控制.如果我们可以将 lambda 用于 for 循环,为什么将 lambda 用于 if 坏主意?

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 有 Optional,类似于我对 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);

哪个更好?而且,哎呀,你有没有注意到在传统的 if 子句代码中,有一个错误:

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!

推荐答案

因为它几乎但不是真正匹配 Optional,也许你可能会重新考虑逻辑:

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));

这里的 map 可能会限制元素的视图:

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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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