使用可选和映射获取和设置 java 8 中的值
Getting and Setting values in java 8 using optional and map
我试图在 java
中使用 Optional 而不是标准的 null 检查
@Data
public class InputObj {
private Double savings;
}
@Data
public class Result {
private String outputSavings;
}
public Result convertInputObjToResult(InputObj inputObj){
Result result = new Result();
Optional<InputObj> optionalInputObj = Optional.ofNullable(inputObj);
optionalInputObj.map(InputObj::getSavings).map(value -> util.convertRoundAndAbs(value,true)).ifPresent(result::setOutputSavings);
return result;
}
相当于下面的
public Result convertInputObjToResult(InputObj inputObj){
Result result = new Result();
if(inputObj != null){
if(inputObj.getSavings() != null){
result.setOutputSavings(util.convertRoundAndAbs(inputObj.getSavings(),true));
}
}
return result;
}
我写了一些测试用例,但我没有得到任何空指针异常,但我无法理解 ifPresent 条件结束而 map 之前,但我仍然没有得到任何 NPE。您是否发现此代码有任何问题或如何对其进行改进?这是 spring 引导应用程序的一部分,@Data 注释用于 lombok。
除了拼写错误“optionalMembershipDetails”外,代码没有任何问题。假设您打算使用“optionalInputObj”。
您需要阅读 java 流以了解完整的概念。简而言之,操作是以惰性方式评估的。所以 ifPresent 调用将触发出现在它之前的操作。在这种情况下,对象被包裹在可选的内部,因此每个中间操作都会将另一个可选的传递给下一个操作。拥有 Optional 可以防止您获得 NPE。
Java 可选 class 的 a link to further describe how the map operation works。
If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. Otherwise return an empty Optional.
因此,如果您从 map
方法获得空值,它将自动转换为 Optional.empty()
。
然后,look at the ifPresent method
If a value is present, invoke the specified consumer with the value, otherwise do nothing.
所以这就是您没有获得任何 NPE 的原因;映射操作能够映射 null
结果 或 将值传递给 Optional.empty()
,如果传递空可选,ifPresent
操作不会执行.
我试图在 java
中使用 Optional 而不是标准的 null 检查@Data
public class InputObj {
private Double savings;
}
@Data
public class Result {
private String outputSavings;
}
public Result convertInputObjToResult(InputObj inputObj){
Result result = new Result();
Optional<InputObj> optionalInputObj = Optional.ofNullable(inputObj);
optionalInputObj.map(InputObj::getSavings).map(value -> util.convertRoundAndAbs(value,true)).ifPresent(result::setOutputSavings);
return result;
}
相当于下面的
public Result convertInputObjToResult(InputObj inputObj){
Result result = new Result();
if(inputObj != null){
if(inputObj.getSavings() != null){
result.setOutputSavings(util.convertRoundAndAbs(inputObj.getSavings(),true));
}
}
return result;
}
我写了一些测试用例,但我没有得到任何空指针异常,但我无法理解 ifPresent 条件结束而 map 之前,但我仍然没有得到任何 NPE。您是否发现此代码有任何问题或如何对其进行改进?这是 spring 引导应用程序的一部分,@Data 注释用于 lombok。
除了拼写错误“optionalMembershipDetails”外,代码没有任何问题。假设您打算使用“optionalInputObj”。 您需要阅读 java 流以了解完整的概念。简而言之,操作是以惰性方式评估的。所以 ifPresent 调用将触发出现在它之前的操作。在这种情况下,对象被包裹在可选的内部,因此每个中间操作都会将另一个可选的传递给下一个操作。拥有 Optional 可以防止您获得 NPE。
Java 可选 class 的 a link to further describe how the map operation works。
If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. Otherwise return an empty Optional.
因此,如果您从 map
方法获得空值,它将自动转换为 Optional.empty()
。
然后,look at the ifPresent method
If a value is present, invoke the specified consumer with the value, otherwise do nothing.
所以这就是您没有获得任何 NPE 的原因;映射操作能够映射 null
结果 或 将值传递给 Optional.empty()
,如果传递空可选,ifPresent
操作不会执行.