英文:
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("<")) {
x = "hello1";
}
else if (x.contains(",")) {
x = "hello2";
} else {
x = "hello3";
}
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("<") ? "hello1"
: x.contains(",") ? "hello2" : "hello3";
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<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";
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论