英文:
IfPresent proper use
问题
我正在尝试在这里使用ifPresent
,但无法使其正常工作。
这是代码:
final Optional<GenericApiGatewayResponse> apiGatewayResponse = getQueryEventsCallResponse(apiGatewayRequest);
apiGatewayResponse.ifPresent(this::getQueryEvents);
private Optional<QueryEventsResponse> getQueryEvents(final GenericApiGatewayResponse apiGatewayResponse) {
try {
return Optional.of(gson.fromJson(apiGatewayResponse.getBody(), QueryEventsResponse.class));
} catch (final Exception e) {
log.error("QueryEventsResponseDeserializationFailure : Failure " +
"while deserialize of QueryEvents from GenericApiGatewayResponse", e);
}
return Optional.empty();
}
private Optional<GenericApiGatewayResponse> getQueryEventsCallResponse(final GenericApiGatewayRequest request) {
try {
return Optional.of(apiGatewayClient.execute(request));
} catch(final Exception e) {
log.error("QueryEventsCallError : Error during invoke of QueryEvents API Gateway", e);
}
return Optional.empty();
}
但我想要ifPresent
的响应作为Optional<QueryEventsResponse>
。但ifPresent
不允许你返回任何东西。
英文:
I am trying to use ifPresent here but not able to get it working.
Here is the code.
final Optional<GenericApiGatewayResponse> apiGatewayResponse = getQueryEventsCallResponse(apiGatewayRequest);
apiGatewayResponse.ifPresent(this::getQueryEvents);
private Optional<QueryEventsResponse> getQueryEvents(final GenericApiGatewayResponse apiGatewayResponse) {
try {
return Optional.of(gson.fromJson(apiGatewayResponse.getBody(), QueryEventsResponse.class));
} catch (final Exception e) {
log.error("QueryEventsResponseDeserializationFailure : Failure " +
"while deserialize of QueryEvents from GenericApiGatewayResponse", e);
}
return Optional.empty();
}
private Optional<GenericApiGatewayResponse> getQueryEventsCallResponse(final GenericApiGatewayRequest request) {
try {
return Optional.of(apiGatewayClient.execute(request));
} catch(final Exception e) {
log.error("QueryEventsCallError : Error during invoke of QueryEvents API Gateway", e);
}
return Optional.empty();
}
But I want to get response of ifPresent as Optional<QueryEventsResponse>. But ifPresent does not allow you to return anything.
答案1
得分: 2
你需要使用的方法是flatMap
。尝试以下代码:
Optional<GenericApiGatewayResponse> apiGatewayResponseOptional = getQueryEventsCallResponse(apiGatewayRequest);
Optional<QueryEventsResponse> queryEventsResponseOptional = apiGatewayResponseOptional.flatMap(this::getQueryEvents);
英文:
The method you need to use is flatMap
. Try the following:
Optional<GenericApiGatewayResponse> apiGatewayResponseOptional = getQueryEventsCallResponse(apiGatewayRequest);
Optional<QueryEventsResponse> queryEventsResponseOptional = apiGatewayResponseOptional.flatmap(this::getQueryEvents);
答案2
得分: 0
Optional.ifPresent()
是一种在存在于 optional 中的值上添加效果的方法。由于在 Java 中不能通过返回值重载方法,因此它不能有任何返回值,因此它保持为 void,并且只允许执行诸如打印 Optional
包含值等副作用。
您仍然可以使用以下方式之一:
if (!optional.isEmpty()) {
this.getQueryEvents(optional.get())
}
或者:
optional.flatMap(containedVal -> this.getQueryEvents(containedVal));
英文:
Optional.ifPresent()
is method to add effect when optional is present over value that is stored inside that optional. It cannot have any return value since in Java you can't overload method's by return value -> therefore it stick's with void and only allow's you to do side effect like printing Optional
contained value for example.
You can still use:
if (!optional.isEmpty()) {
this.getQueryEvents(optional.get())
}
or:
optional.flatMap(containedVal -> this.getQueryEvents(containedVal));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论