如何判断一个枚举值是否存在(Check if an Enum Value Exists in Java)

目录

    • Searching an Enum by Name
    • Searching an Enum by Value
    • Searching an Enum by Integer Value
    • Exceptions Thrown from Search Methods

Searching an Enum by Name

public enum Direction {
    EAST, WEST, SOUTH, NORTH;
    
    public static Direction findByName(String name) {
        Direction result = null;
        for (Direction direction : Direction.values()) {
            if (direction.name().equalsIgnoreCase(name)) {
                result = direction;
                break;
            }
        }
        return result;
    }
}
Direction result1 = Direction .findByName("EAST");
Direction result2 = Direction .findByName("east");
Direction result3 = Direction .findByName("East");
System.out.println(Direction.EAST == result1); // true
System.out.println(Direction.EAST == result2); // true
System.out.println(Direction.EAST == result3); // true

Searching an Enum by Value

public enum Weekday {
    MONDAY("Monday"), TUESDAY("Tuesday"), SUNDAY("Sunday");
    
    private final String value;
    
    Weekday(String value) {
        this.value = value;
    }
    
    public String getValue() {
        return value;
    }
    
    public static Weekday findByValue(String value) {
        Weekday result = null;
        for (Weekday day : Weekday.values()) {
            if (day.getValue().equalsIgnoreCase(value)) {
                result = day;
                break;
            }
        }
        return result;
    }
}
Weekday result1 = Weekday.findByValue("Monday");
System.out.println(Weekday.MONDAY == result1); // true

Weekday result2 = Weekday.findByValue("mon");
System.out.println(result2); // null

Searching an Enum by Integer Value

public enum Month {
    JANUARY("January", 1), 
    FEBRUARY("February", 2), 
    // ...
    DECEMBER("December", 12);
    
    private final String value;
    private final int code;
    
    Month(String value, int code) {
        this.value = value;
        this.code = code;
    }
    
    public String getValue() {
        return value;
    }
    public int getCode() {
        return code;
    }
    
    public static Optional<Month> findByCode(int code) {
        return Arrays.stream(values()).filter(month -> month.getCode() == code).findFirst();
    }
}
Optional<Month> result = Month.findByCode(1);
System.out.println(Month.JANUARY == result.orElseGet(() -> null)); // true

Optional<Month> result2 = Month.findByCode(0);
System.out.println(result2.orElseGet(() -> null)); // null

Exceptions Thrown from Search Methods

public enum Month {
    JANUARY("January", 1),
    FEBRUARY("February", 2),
    // ...
    DECEMBER("December", 12);
    
    private final String value;
    private final int code;
    
    Month(String value, int code) {
        this.value = value;
        this.code = code;
    }
    
    public String getValue() {
        return value;
    }
    public int getCode() {
        return code;
    }
    
    public static Optional<Month> findByCode(int code) {
        return Arrays.stream(values()).filter(month -> month.getCode() == code).findFirst();
    }
    
    public static Month findByValue(String value) {
        return Arrays.stream(values()).filter(month -> month.getValue().equalsIgnoreCase(value)).findFirst().orElseThrow(IllegalArgumentException::new);
    }
}
Month.findByValue("Jan");

Exception in thread "main" java.lang.IllegalArgumentException
	at java.base/java.util.Optional.orElseThrow(Optional.java:403)
	at javabasic.enumprac.CheckEnumValueExist$Month.findByValue(CheckEnumValueExist.java:77)
	at javabasic.enumprac.CheckEnumValueExist.main(CheckEnumValueExist.java:85)

-----------------------------------------------------------------------------读书笔记摘自 文章:Check if an Enum Value Exists in Java

你可能感兴趣的:(Baeldung,学习笔记,java,开发语言)