Java 8 可选:ifPresent return object orElseThrow exception
Java 8 optional: ifPresent return object orElseThrow exception
我正在尝试制作这样的东西:
private String getStringIfObjectIsPresent(Optional<Object> object){
object.ifPresent(() ->{
String result = "result";
//some logic with result and return it
return result;
}).orElseThrow(MyCustomException::new);
}
这个不行,因为ifPresent的参数是Consumer函数接口,参数是void accept(T t)。它不能 return 任何值。还有其他方法吗?
改用 map
函数。它转换可选内部的值。
像这样:
private String getStringIfObjectIsPresent(Optional<Object> object) {
return object.map(() -> {
String result = "result";
//some logic with result and return it
return result;
}).orElseThrow(MyCustomException::new);
}
您实际搜索的是:Optional.map。您的代码将如下所示:
object.map(o -> "result" /* or your function */)
.orElseThrow(MyCustomException::new);
如果可以的话,我宁愿不传递 Optional
。最后,您在这里使用 Optional
什么也得不到。稍微不同的变体:
public String getString(Object yourObject) {
if (Objects.isNull(yourObject)) { // or use requireNonNull instead if NullPointerException suffices
throw new MyCustomException();
}
String result = ...
// your string mapping function
return result;
}
如果由于另一个调用你已经有了 Optional
-object,我仍然建议你使用 map
-method,而不是 isPresent
,等等。一个原因,我发现它更具可读性(显然是一个主观决定 ;-))。
这里有两个选项:
将ifPresent
替换为map
并使用Function
代替Consumer
private String getStringIfObjectIsPresent(Optional<Object> object) {
return object
.map(obj -> {
String result = "result";
//some logic with result and return it
return result;
})
.orElseThrow(MyCustomException::new);
}
使用isPresent
:
private String getStringIfObjectIsPresent(Optional<Object> object) {
if (object.isPresent()) {
String result = "result";
//some logic with result and return it
return result;
} else {
throw new MyCustomException();
}
}
在确定值可用后我更喜欢映射
private String getStringIfObjectIsPresent(Optional<Object> object) {
Object ob = object.orElseThrow(MyCustomException::new);
// do your mapping with ob
String result = your-map-function(ob);
return result;
}
或一班
private String getStringIfObjectIsPresent(Optional<Object> object) {
return your-map-function(object.orElseThrow(MyCustomException::new));
}
你举的例子不是很好的例子。可选不应该作为参数发送到另一个函数。好的做法是始终将 non-null 参数发送到函数中。这样我们就知道输入不会为空。这可以减少我们的代码不确定性。
我正在尝试制作这样的东西:
private String getStringIfObjectIsPresent(Optional<Object> object){
object.ifPresent(() ->{
String result = "result";
//some logic with result and return it
return result;
}).orElseThrow(MyCustomException::new);
}
这个不行,因为ifPresent的参数是Consumer函数接口,参数是void accept(T t)。它不能 return 任何值。还有其他方法吗?
改用 map
函数。它转换可选内部的值。
像这样:
private String getStringIfObjectIsPresent(Optional<Object> object) {
return object.map(() -> {
String result = "result";
//some logic with result and return it
return result;
}).orElseThrow(MyCustomException::new);
}
您实际搜索的是:Optional.map。您的代码将如下所示:
object.map(o -> "result" /* or your function */)
.orElseThrow(MyCustomException::new);
如果可以的话,我宁愿不传递 Optional
。最后,您在这里使用 Optional
什么也得不到。稍微不同的变体:
public String getString(Object yourObject) {
if (Objects.isNull(yourObject)) { // or use requireNonNull instead if NullPointerException suffices
throw new MyCustomException();
}
String result = ...
// your string mapping function
return result;
}
如果由于另一个调用你已经有了 Optional
-object,我仍然建议你使用 map
-method,而不是 isPresent
,等等。一个原因,我发现它更具可读性(显然是一个主观决定 ;-))。
这里有两个选项:
将ifPresent
替换为map
并使用Function
代替Consumer
private String getStringIfObjectIsPresent(Optional<Object> object) {
return object
.map(obj -> {
String result = "result";
//some logic with result and return it
return result;
})
.orElseThrow(MyCustomException::new);
}
使用isPresent
:
private String getStringIfObjectIsPresent(Optional<Object> object) {
if (object.isPresent()) {
String result = "result";
//some logic with result and return it
return result;
} else {
throw new MyCustomException();
}
}
在确定值可用后我更喜欢映射
private String getStringIfObjectIsPresent(Optional<Object> object) {
Object ob = object.orElseThrow(MyCustomException::new);
// do your mapping with ob
String result = your-map-function(ob);
return result;
}
或一班
private String getStringIfObjectIsPresent(Optional<Object> object) {
return your-map-function(object.orElseThrow(MyCustomException::new));
}
你举的例子不是很好的例子。可选不应该作为参数发送到另一个函数。好的做法是始终将 non-null 参数发送到函数中。这样我们就知道输入不会为空。这可以减少我们的代码不确定性。