Java 枚举字符串:从基础到高级应用 1. 枚举基础与字符串表示在 Java 中枚举Enum是一种特殊的类用于定义一组固定的常量。每个枚举常量都有一个默认的字符串表示形式即其名称。public enum Color { RED, GREEN, BLUE } public class Main { public static void main(String[] args) { Color color Color.RED; System.out.println(color); // 输出: RED System.out.println(color.name()); // 输出: RED } }枚举的toString()方法默认返回枚举常量的名称与name()方法返回的结果相同。但toString()可以被重写而name()是 final 方法不能被重写。2. 枚举与字符串的相互转换2.1 字符串转枚举使用Enum.valueOf()方法可以将字符串转换为对应的枚举常量String colorStr GREEN; Color color Color.valueOf(colorStr); // 返回 Color.GREEN System.out.println(color); // 输出: GREEN注意事项字符串必须与枚举常量的名称完全匹配区分大小写如果字符串不匹配任何枚举常量会抛出IllegalArgumentException可以使用try-catch处理异常或先检查字符串是否有效2.2 枚举转字符串除了使用toString()和name()方法还可以为枚举添加自定义属性public enum Status { ACTIVE(激活), INACTIVE(未激活), DELETED(已删除); private final String description; Status(String description) { this.description description; } public String getDescription() { return description; } Override public String toString() { return description; } } public class Main { public static void main(String[] args) { Status status Status.ACTIVE; System.out.println(status.name()); // 输出: ACTIVE System.out.println(status.toString()); // 输出: 激活 System.out.println(status.getDescription()); // 输出: 激活 } }3. 高级应用场景3.1 枚举映射到数据库值在实际开发中经常需要将枚举值存储到数据库。可以使用自定义的转换器public enum UserType { ADMIN(A, 管理员), USER(U, 普通用户), GUEST(G, 访客); private final String dbCode; private final String displayName; UserType(String dbCode, String displayName) { this.dbCode dbCode; this.displayName displayName; } public String getDbCode() { return dbCode; } public String getDisplayName() { return displayName; } // 根据数据库代码获取枚举 public static UserType fromDbCode(String dbCode) { for (UserType type : values()) { if (type.dbCode.equals(dbCode)) { return type; } } throw new IllegalArgumentException(无效的用户类型代码: dbCode); } }3.2 枚举与 JSON 序列化在使用 JSON 序列化框架如 Jackson、Gson时可以自定义枚举的序列化和反序列化方式import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; public enum Priority { HIGH(high, 1), MEDIUM(medium, 2), LOW(low, 3); private final String jsonValue; private final int level; Priority(String jsonValue, int level) { this.jsonValue jsonValue; this.level level; } JsonValue public String getJsonValue() { return jsonValue; } JsonCreator public static Priority fromJsonValue(String jsonValue) { for (Priority priority : values()) { if (priority.jsonValue.equals(jsonValue)) { return priority; } } throw new IllegalArgumentException(无效的优先级: jsonValue); } }3.3 枚举与 Spring 请求参数在 Spring MVC 中可以直接使用枚举作为请求参数RestController public class UserController { GetMapping(/users) public ListUser getUsers(RequestParam UserType type) { // type 会自动从字符串转换为对应的枚举 return userService.findByType(type); } }Spring 默认使用Enum.valueOf()进行转换如果需要自定义转换逻辑可以实现ConverterString, Enum接口。4. 最佳实践与注意事项使用 name() 进行序列化当需要稳定的字符串表示时使用name()而不是toString()重写 toString() 提供友好显示为终端用户显示时可以重写toString()返回更友好的描述添加 fromString() 方法为枚举添加静态的fromString()方法提供更灵活的反序列化处理未知值在反序列化时考虑添加一个UNKNOWN枚举常量来处理未知的字符串值性能考虑Enum.valueOf()的时间复杂度是 O(n)对于频繁调用的场景可以考虑使用 Map 缓存5. 总结Java 枚举与字符串的转换是日常开发中的常见需求。掌握name()、toString()、valueOf()等核心方法并根据实际场景为枚举添加自定义属性和转换方法可以大大提高代码的可读性和可维护性。无论是数据库存储、JSON 序列化还是 Web 请求处理合理的枚举设计都能让代码更加优雅和健壮。