Java 如果不为null,则返回值
我尽量少写几行。 用Java重新编写此文件的最佳方法是什么?(假设我们有一个“公共枚举a”) 我想有一种写“如果某物不是空的,返回它,否则,继续”的方法。 我研究了Optional,但Optional.of(…).ifPresent(v->return v;)不起作用,尽管我希望它能起作用Java 如果不为null,则返回值,java,java-8,optional,Java,Java 8,Optional,我尽量少写几行。 用Java重新编写此文件的最佳方法是什么?(假设我们有一个“公共枚举a”) 我想有一种写“如果某物不是空的,返回它,否则,继续”的方法。 我研究了Optional,但Optional.of(…).ifPresent(v->return v;)不起作用,尽管我希望它能起作用 此外,如果你问我为什么要在for循环中改变数组,你可以考虑有多个初始数组,我需要对每个数组进行测试。 public A getAValue() { int[] arr = new int[7];
此外,如果你问我为什么要在for循环中改变数组,你可以考虑有多个初始数组,我需要对每个数组进行测试。
public A getAValue() {
int[] arr = new int[7];
A returnValue = null;
for (int i=0; i<10; i++) {
//change arr
returnValue = checkSomeCondition(arr);
if (returnValue != null) {
return returnValue;
}
//change arr again
returnValue = checkSomeCondition(arr);
if (returnValue != null) {
return returnValue;
}
//change arr yet again
returnValue = checkSomeCondition(arr);
if (returnValue != null) {
return returnValue;
}
}
return A.UNKNOWN;
}
public A checkSomeCondition(int[] arr) {
if (arr[0]==arr[1]) {
return arr[0]==2 ? A.VALUE_1 : A.VALUE_2;
}
return null;
}
public A getAValue(){
int[]arr=新int[7];
返回值=null;
对于(int i=0;i使用Optional.ofNullable
而不是Optional.of
,因为后者不能用于可能的空值。这将起作用,并将该任务委托给checkSomeCondition
函数:
public Optional<A> checkSomeCondition(int[] arr) {
if (arr[0] == arr[1]) {
return Optional.<A>of(arr[0]==2 ? A.VALUE_1 : A.VALUE_2);
}
return Optional.<A>empty();
}
不清楚您正在进行什么样的测试或如何更改阵列,但这可能是可能的:
这应该忽略所有null
返回值,只返回
第一个成功的测试结果
如果没有成功,则必须提供一个默认值
返回对象
enum A {
A_VALUE_1, A_VALUE_2, A_UNKNOWN
};
List<Object[]> testArrays = new ArrayList<>();
testArrays.add(new int[] { 1, 2, 3 });
testArrays.add(new int[] { 3, 4, 5 });
testArrays.add(new int[] { 2, 2, 6 });
A res =
// stream the set of arrays
testArrays.stream()
// apply each set in order to the method
// replacing the array under test with the
// method result
.map(arr->checkSomeCondition(arr))
// filter out any null results
.filter(result->result!=null)
// get the first non-null result
.findFirst()
// return the result or some default answer if all
// results were null.
.orElseGet(()->A.A_UNKNOWN);
System.out.println(res);
你的测试方法
public A checkSomeCondition(int[] arr) {
if (arr[0] == arr[1]) {
return arr[0] == 2 ? A.A_VALUE_1 : A.A_VALUE_2;
}
return null;
}
不利的一面是,它要求您提供一个阵列,如果上一次测试失败,则将在其中使用后续阵列。您可以根据自己的需求调整该概念
Q:我研究了可选的,但可选的。of(…)。ifPresent(v->return v;)不起作用,尽管我希望它能起作用
工作正常,但与预期不同。返回
在使用者
的方法实现中。此外,使用者
不返回任何内容,方法返回类型为
Q:我正在尽量少写几行
您可以通过一组预定义的数组suppliersSupplier
和现有数组中的另一个for循环来实现所需的功能,其迭代次数与许多条件相同
List<Supplier<int[]>> arrays = new ArrayList<>();
arrays.add(() -> firstArraySource());
arrays.add(() -> secondArraySource());
...
for (...) { // your initial loop
for (Supplier<int[]> arraySupplier: arrays) { // iterate through Suppliers
arr = arraySupplier.get(); // get an array (here is the evaluation)
returnValue = checkSomeCondition(arr); // getting the 'A' value
if (returnValue != null) { // returnin if not null
return returnValue;
}
}
}
List arrays=new ArrayList();
add(()->firstArraySource());
add(()->secondarrarysource());
...
对于(…){//初始循环
对于(供应商阵列供应商:阵列){//遍历供应商
arr=arraySupplier.get();//获取一个数组(这是计算结果)
returnValue=checkSomeCondition(arr);//获取“A”值
if(returnValue!=null){//returnin如果不为null
返回值;
}
}
}
这种方法的优点是,如果数组的源是从数据库中导出的,那么在实际需要它们之前就不会调用它们,因为它们被包装在供应商中。当调用Supplier::get
时,就会发生计算。使用Optional是可以的,但OP仍然必须具有相同的结构,因此不会减少代码。感谢您的帮助。我将研究解决方案。同时,以下是代码:。这是一个tic-tac-toe:)
public A checkSomeCondition(int[] arr) {
if (arr[0] == arr[1]) {
return arr[0] == 2 ? A.A_VALUE_1 : A.A_VALUE_2;
}
return null;
}
List<Supplier<int[]>> arrays = new ArrayList<>();
arrays.add(() -> firstArraySource());
arrays.add(() -> secondArraySource());
...
for (...) { // your initial loop
for (Supplier<int[]> arraySupplier: arrays) { // iterate through Suppliers
arr = arraySupplier.get(); // get an array (here is the evaluation)
returnValue = checkSomeCondition(arr); // getting the 'A' value
if (returnValue != null) { // returnin if not null
return returnValue;
}
}
}