java - 如何使用 Java 中的 if 语句检查字符串是否为空或 null?
问题描述
我需要先通过使用来获取枚举本身RegoDocumentType.getByValue(createOrderRequest.getIdDocument().getIdType())
然后检查空值,如果不为空,则返回枚举值。否则,它将返回createOrderRequest.getIdDocument().getIdType()
默认值。
那么,如何使用 RegoDocumentType 枚举重构代码以仅使用一个 if 语句来满足 NRIC/11B 和 FIN smag 值到 rego 值?
这是我的枚举:
public enum RegoDocumentType {
NRIC_11B("NRIC/11B", IdentityType.NRIC.toValue()),
FIN("Employment Pass", IdentityType.EM_PASS.toValue()),
;
private static final Map<String, RegoDocumentType> BY_SMAG_VALUE = new HashMap<>();
static {
for (RegoDocumentType identityType : values()) {
BY_SMAG_VALUE.put(identityType.getSmagValue().toLowerCase(), identityType);
}
}
private final String smagValue;
private final String regoValue;
RegoDocumentType(String smagValue, String regoValue) {
this.smagValue = smagValue;
this.regoValue = regoValue;
}
public String getSmagValue() {
return smagValue;
}
public String getRegoValue() {
return regoValue;
}
public static RegoDocumentType getBySmagValue(String smagValue)
{ return BY_SMAG_VALUE.get(smagValue.toLowerCase()); }
}
解决方案
String something = "";
if(something == null || something.isEmpty()) {
// return A
} else {
// return B
}
or
String something = "";
return something == null || something.isEmpty() ? [value A] : [value B];
推荐阅读
- java - 实现接口以调用相同方法的类数组
- arrays - Stat.exists false 与 ansible 中的变量列表
- java - Spring Boot JPA - 无法获取分页嵌套资源
- objective-c - CLLocationManager didRangeBeacons 停止在前台工作
- xtext - 在 XText Eclipse DSL 工具版本中生成 EMF 模型代码的异常:2019-03 RC1 (4.11 RC1) - Windows 10
- reactjs - 通过道具传递给孩子的反应执行委托
- javascript - 处理垂直文本区域
- python - Python 在路径中,但仍未在 Windows 10 中打开 [python]
- bash - 错误:分配关联数组时必须使用下标
- sql - MariaDB 查询中“字段列表”中的未知列