如何修复 Java 9 可选 "Cannot return a void result" 错误消息?
How to fix Java 9 Optional "Cannot return a void result" error message?
我有一个 class 方法是这样的:
public class Client {
private project.enums.ClientType clientType;
private ClientType clientTypeV2;
@JsonIgnore
public Optional<Integer> getCodeClientTypeV2() {
return Optional.ofNullable(this.clientTypeV2).map(ClientType::getCode);
}
}
但是我想改变这个方法的逻辑。我希望如果 clientTypeV2
已填充,则它 return 是该对象的 code
。否则,我希望它成为 clientType
枚举中的 return code
。如何使用 java 8 做到这一点?我尝试了以下代码,但出现错误消息 "Cannot return a void result"
@JsonIgnore
public Optional<Integer> getCodeClientTypeV2() {
return Optional.ofNullable(this.clientTypeV2).ifPresentOrElse(ClientType::getCode, () -> this.clientType.getCode());
}
#编辑 1
我试过这个:
@JsonIgnore
public Integer getCodeClientTypeV2() {
return Optional.ofNullable(this.clientTypeV2)
.map(ClientType::getCode)
.orElse(this.clientType.getCode()) ;
}
在debug中,虽然clientTypeV2被填满了,但是由于clientType为null,执行流程进入了orElse内部,抛出了NullPointerException。我错过了什么?
按照惯例,如果您使用 orElse
,您将返回一个常量,保证存在 值 。 orElse
要么解包 Optional
中包含的值,要么使用您提供的默认值。
如果您不想更改方法签名,请改用 Optional#or
。您必须对 clientType
对象的空值检查保持智能,因为如果它不存在,您就不能依赖它作为具体的可返回对象。
return Optional.ofNullable(this.clientTypeV2)
.map(ClientType::getCode)
.or(this.clientType.getCode());
根据getCode
能不能returnnull
.
有不同的解决方法
当你不想预先计算替代表达式时,你必须使用 orElseGet(Supplier<? extends T> other)
而不是 orElse(T other)
。
return Optional.ofNullable(clientTypeV2).map(ClientType::getCode)
.orElseGet(() -> clientType.getCode());
如果 getCode
不能 return null
而您只想处理 clientTypeV2
或 clientType
可以 [=14] 的可能性=],你也可以使用
return Optional.ofNullable(clientTypeV2).orElse(clientType).getCode();
甚至更简单
return (clientTypeV2 != null? clientTypeV2: clientType).getCode()
所有解决方案的共同点是假设 clientTypeV2
或 clientType
中至少有一个不是 null
。
我有一个 class 方法是这样的:
public class Client {
private project.enums.ClientType clientType;
private ClientType clientTypeV2;
@JsonIgnore
public Optional<Integer> getCodeClientTypeV2() {
return Optional.ofNullable(this.clientTypeV2).map(ClientType::getCode);
}
}
但是我想改变这个方法的逻辑。我希望如果 clientTypeV2
已填充,则它 return 是该对象的 code
。否则,我希望它成为 clientType
枚举中的 return code
。如何使用 java 8 做到这一点?我尝试了以下代码,但出现错误消息 "Cannot return a void result"
@JsonIgnore
public Optional<Integer> getCodeClientTypeV2() {
return Optional.ofNullable(this.clientTypeV2).ifPresentOrElse(ClientType::getCode, () -> this.clientType.getCode());
}
#编辑 1
我试过这个:
@JsonIgnore
public Integer getCodeClientTypeV2() {
return Optional.ofNullable(this.clientTypeV2)
.map(ClientType::getCode)
.orElse(this.clientType.getCode()) ;
}
在debug中,虽然clientTypeV2被填满了,但是由于clientType为null,执行流程进入了orElse内部,抛出了NullPointerException。我错过了什么?
按照惯例,如果您使用 orElse
,您将返回一个常量,保证存在 值 。 orElse
要么解包 Optional
中包含的值,要么使用您提供的默认值。
如果您不想更改方法签名,请改用 Optional#or
。您必须对 clientType
对象的空值检查保持智能,因为如果它不存在,您就不能依赖它作为具体的可返回对象。
return Optional.ofNullable(this.clientTypeV2)
.map(ClientType::getCode)
.or(this.clientType.getCode());
根据getCode
能不能returnnull
.
当你不想预先计算替代表达式时,你必须使用 orElseGet(Supplier<? extends T> other)
而不是 orElse(T other)
。
return Optional.ofNullable(clientTypeV2).map(ClientType::getCode)
.orElseGet(() -> clientType.getCode());
如果 getCode
不能 return null
而您只想处理 clientTypeV2
或 clientType
可以 [=14] 的可能性=],你也可以使用
return Optional.ofNullable(clientTypeV2).orElse(clientType).getCode();
甚至更简单
return (clientTypeV2 != null? clientTypeV2: clientType).getCode()
所有解决方案的共同点是假设 clientTypeV2
或 clientType
中至少有一个不是 null
。