java:使用Optional优化Java中的if语句

huangapple go评论63阅读模式
英文:

java: Optimizing if statement in Java using Optional

问题

以下是你想要的翻译部分:

我在Java中有如下的代码,我需要通过使用Java 8中的Optional来进行优化:

if (x.isEmpty() || x.contains("<")) {
   x = "hello1";
}
else if (x.contains(",")) {
   x = "hello2";
} else {
   x = "hello3";
}

有人可以提供一个基于Java 8的,使用Optional<String>的代码吗?

我不想使用多个if-else语句。我更倾向于使用一些更加函数式的东西,就像在Java 8中的Optional一样,因为if-else更多地是命令式的风格。所以我可以更正地说,我的目标不是在于代码性能方面的优化,而是使其符合更加函数式的Java 8标准。

英文:

I have the following code in Java which I need to optimize by the usage of Optional in Java 8:

if (x.isEmpty() || x.contains(&quot;&lt;&quot;)) {
   x = &quot;hello1&quot;;
}
else if (x.contains(&quot;,&quot;)) {
   x = &quot;hello2&quot;;
} else {
   x = &quot;hello3&quot;;
}

Can someone suggest a Java 8 based code using Optional<String>?

I don't want to use multiple if-else. I prefer using something more functional like Optional in java 8 since if-else is more imperative style. So I can correct and say my objective is not in terms of code optimization in terms of performance, but making it use Java 8 standards which are more functional in nature.

答案1

得分: 1

x = x.isEmpty() || x.contains("<") ? "hello1"
: x.contains(",") ? "hello2" : "hello3";

如果你追求函数式风格,就像你可以在某些情况下使用Optional,那么在这种情况下你仍然不需要使用Optional,而是使用经典的条件运算符(有时称为三元运算符;它从Java 1.0开始就存在)。在你的代码中没有明确的将Optional融入其中的有意义的方法。

英文:
	x = x.isEmpty() || x.contains(&quot;&lt;&quot;) ? &quot;hello1&quot; 
			: x.contains(&quot;,&quot;) ? &quot;hello2&quot; : &quot;hello3&quot;;

If you’re after the functional style that you can in some cases have with Optional, then you still don’t need an Optional in this case, but rather the good old conditional operator (sometimes called the ternary operator; it was there since Java 1.0). There would be no meaningful way to fit an Optional into your code.

答案2

得分: -4

我已尝试过这种方法。它可以使您的代码更加清晰和面向对象。当存在许多 if/else 代码块导致混淆时,可以使用此模式。希望这正是您所寻找的。

package com.test;

import java.util.Optional;

public class Refactor {

    String x;

    public Refactor(String x) {
        this.x = x;
    }

    Optional<String> condition1() {
        if (x.isEmpty() || x.contains("<")) {
            return Optional.of("hello1");
        }
        return Optional.ofNullable(null);
    }

    Optional<String> condition2() {
        if (x.contains(",")) {
            return Optional.of("hello2");
        }
        return Optional.ofNullable(null);
    }

    public String conditionTest() {
        Refactor refactor = new Refactor("ab<cd");
        if (refactor.condition1().isPresent()) {
            return refactor.condition1().get();
        }
        if (refactor.condition2().isPresent()) {
            return refactor.condition2().get();
        }
        return "hello3";
    }
}
英文:

I have tried this approach. It is making your code more cleaner and Object-oriented . This pattern can be used when there is lot of if/else block which leads to confusion. Hope this is what you are looking for.

package com.test;

import java.util.Optional;

public class Refactor {

String x;

public Refactor(String x) {
this.x = x;
}

Optional&lt;String&gt; condition1() {
	if (x.isEmpty() || x.contains(&quot;&lt;&quot;)) {
		   return Optional.of(&quot;hello1&quot;);
		}
	return Optional.ofNullable(null);
}

Optional&lt;String&gt; condition2() {
	if (x.contains(&quot;,&quot;)) {
		   return Optional.of(&quot;hello2&quot;);
		}
	return Optional.ofNullable(null);
}

public String  conditionTest() {
	Refactor refactor = new Refactor(&quot;ab&lt;cd&quot;);
	if(refactor.condition1().isPresent()) {
		return refactor.condition1().get();
	}
	if(refactor.condition2().isPresent()) {
		return refactor.condition2().get();
	}
	return &quot;hello3&quot;;
}

}

huangapple
  • 本文由 发表于 2020年9月25日 08:16:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/64056071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定