如何在Java中使用if语句检查字符串是否为空?

如何在Java中使用if语句检查字符串是否为空?,java,if-statement,null,apache-stringutils,Java,If Statement,Null,Apache Stringutils,我需要首先使用RegoDocumentType.getByValue(createOrderRequest.getIdDocument().getIdType())获取枚举本身。 然后检查null值,如果它不是null,它将返回枚举值。否则,它将返回createOrderRequest.getIdDocument().getIdType()作为默认值 那么,我如何重构代码,使之只使用一条if语句,以便使用RegoDocumentType enum将NRIC/11B和FIN smag值转换为reg

我需要首先使用
RegoDocumentType.getByValue(createOrderRequest.getIdDocument().getIdType())获取枚举本身。

然后检查null值,如果它不是null,它将返回枚举值。否则,它将返回
createOrderRequest.getIdDocument().getIdType()
作为默认值

那么,我如何重构代码,使之只使用一条if语句,以便使用RegoDocumentType enum将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()); }
}
public enum RegoDocumentType{
NRIC_11B(“NRIC/11B”,IdentityType.NRIC.toValue()),
FIN(“就业证”,IdentityType.EM_Pass.toValue()),
;
私有静态最终映射按_SMAG_值=new HashMap();
静止的{
对于(RegoDocumentType identityType:values()){
BY_SMAG_VALUE.put(identityType.getSmagValue().toLowerCase(),identityType);
}
}
私有最终字符串smagValue;
私有最终字符串regoValue;
RegoDocumentType(字符串smagValue、字符串regoValue){
this.smagValue=smagValue;
this.regoValue=regoValue;
}
公共字符串getSmagValue(){
返回smagValue;
}
公共字符串getRegoValue(){
返回regoValue;
}
公共静态RegoDocumentType getBySmagValue(字符串smagValue)
{returnby_SMAG_VALUE.get(smagValue.toLowerCase());}
}


createOrderRequest.getIdDocument().getIdType()
在您的案例中似乎是一个字符串。您不能从
getBySmagValue
返回字符串。考虑返回null或抛出异常:

public static RegoDocumentType getBySmagValue(String smagValue) { 
    if (smagValue == null) reutrn null;
    return BY_SMAG_VALUE.get(smagValue.toLowerCase()); 
}


你是说
RegoDocumentType。getBySmagValue(createOrderRequest.getIdDocument().getIdType())
?哦,是的,这就是我的意思。很抱歉输入错误。您是否希望
createOrderRequest.getIdDocument().getIdType()
可以为null,或者您是否希望
createOrderRequest.getIdDocument().getIdType()
值不在预定义的枚举值中?我希望createOrderRequest.getIdDocument().getIdType()可以为null。从您的示例中,您需要的逻辑是:
如果createOrderRequest.getOrderId()不为null,并且createOrderRequest.getIdDocument().getIdType()为“FIN”,则返回“就业通行证”,否则返回createOrderRequest.getIdDocument().getIdType()
这就是您需要的吗?如果
createOrderRequest.getIdDocument().getIdType()
为空,该怎么办?
    String something = "";
    return something == null || something.isEmpty() ? [value A] : [value B];
public static RegoDocumentType getBySmagValue(String smagValue) { 
    if (smagValue == null) reutrn null;
    return BY_SMAG_VALUE.get(smagValue.toLowerCase()); 
}
public static RegoDocumentType getBySmagValue(String smagValue) { 
    if (smagValue == null) throw new IllegalStateException();
    return BY_SMAG_VALUE.get(smagValue.toLowerCase()); 
}