Java嵌套式种族分类枚举实现方案合理性咨询及优化建议
实现带主/子分类的种族枚举方案优化
现有方案的问题分析
你的方案能够实现目标调用逻辑,但存在冗余设计:外层Ethnicity类+内部枚举+Lombok Builder的组合完全没必要——枚举本身是全局唯一的常量,不需要额外创建Ethnicity实例来持有枚举值,这增加了不必要的对象构建步骤。另外统计主分类人数时,还需要手动处理内部枚举与主分类的关联,流程相对繁琐。
推荐优化方案
方案一:主分类枚举嵌套子分类枚举(贴合Ethnicity.WHITE.BRITISH调用需求)
通过顶层枚举定义主分类,每个主分类枚举内部嵌套对应的子分类枚举,同时子分类持有父分类引用,方便统计关联:
public enum Ethnicity { WHITE("白色人种") { @Override public SubEthnicity[] getSubTypes() { return SubEthnicity.values(); } // 白色人种子分类 public enum SubEthnicity implements SubType { BRITISH("英国白人"), IRISH("爱尔兰白人"), OTHER_WHITE("其他白人"); private final String label; SubEthnicity(String label) { this.label = label; } @Override public String getLabel() { return label; } @Override public Ethnicity getParentGroup() { return WHITE; } } }, BLACK("黑色人种") { @Override public SubEthnicity[] getSubTypes() { return SubEthnicity.values(); } // 黑色人种子分类 public enum SubEthnicity implements SubType { AFRICAN("非洲黑人"), CARIBBEAN("加勒比黑人"), OTHER_BLACK("其他黑人"); private final String label; SubEthnicity(String label) { this.label = label; } @Override public String getLabel() { return label; } @Override public Ethnicity getParentGroup() { return BLACK; } } }, ASIAN("亚洲人种") { @Override public SubEthnicity[] getSubTypes() { return SubEthnicity.values(); } // 亚洲人种子分类 public enum SubEthnicity implements SubType { INDIAN("印度人"), PAKISTANI("巴基斯坦人"), CHINESE("中国人"), OTHER_ASIAN("其他亚洲人"); private final String label; SubEthnicity(String label) { this.label = label; } @Override public String getLabel() { return label; } @Override public Ethnicity getParentGroup() { return ASIAN; } } }; private final String label; Ethnicity(String label) { this.label = label; } public String getLabel() { return label; } // 抽象方法,获取当前主分类下的所有子分类 public abstract SubType[] getSubTypes(); // 子分类统一接口,保证类型一致性 public interface SubType { String getLabel(); Ethnicity getParentGroup(); } }
调用方式:Ethnicity.WHITE.SubEthnicity.BRITISH,完全符合你的预期格式。
统计逻辑示例:
import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; // 假设有一组子分类数据 List<Ethnicity.SubType> data = Arrays.asList( Ethnicity.WHITE.SubEthnicity.BRITISH, Ethnicity.BLACK.SubEthnicity.AFRICAN, Ethnicity.WHITE.SubEthnicity.IRISH, Ethnicity.ASIAN.SubEthnicity.CHINESE, Ethnicity.WHITE.SubEthnicity.BRITISH ); // 按主分类统计人数 Map<Ethnicity, Long> countByGroup = data.stream() .collect(Collectors.groupingBy(Ethnicity.SubType::getParentGroup, Collectors.counting())); // 输出结果 countByGroup.forEach((group, count) -> System.out.println(group.getLabel() + ": " + count + "人"));
方案二:单枚举类关联主分类(更简洁,统计更高效)
如果对调用格式的严格性可以适当放宽,这种方案代码更简洁,统计逻辑更直接:
import java.util.Arrays; import java.util.Map; import java.util.stream.Collectors; public enum Ethnicity { // 白色人种子项 BRITISH("英国白人", Group.WHITE), IRISH("爱尔兰白人", Group.WHITE), OTHER_WHITE("其他白人", Group.WHITE), // 黑色人种子项 AFRICAN("非洲黑人", Group.BLACK), CARIBBEAN("加勒比黑人", Group.BLACK), OTHER_BLACK("其他黑人", Group.BLACK), // 亚洲人种子项 INDIAN("印度人", Group.ASIAN), PAKISTANI("巴基斯坦人", Group.ASIAN), CHINESE("中国人", Group.ASIAN), OTHER_ASIAN("其他亚洲人", Group.ASIAN); private final String label; private final Group parentGroup; Ethnicity(String label, Group parentGroup) { this.label = label; this.parentGroup = parentGroup; } public String getLabel() { return label; } public Group getParentGroup() { return parentGroup; } // 主分类枚举 public enum Group { WHITE("白色人种"), BLACK("黑色人种"), ASIAN("亚洲人种"); private final String label; Group(String label) { this.label = label; } public String getLabel() { return label; } } // 内置统计工具方法 public static Map<Group, Long> countByGroup(Ethnicity[] ethnicities) { return Arrays.stream(ethnicities) .collect(Collectors.groupingBy(Ethnicity::getParentGroup, Collectors.counting())); } }
调用方式:Ethnicity.BRITISH,虽然不是Ethnicity.WHITE.BRITISH的格式,但代码更紧凑,统计时直接调用内置方法即可:
Ethnicity[] data = {Ethnicity.BRITISH, Ethnicity.AFRICAN, Ethnicity.BRITISH, Ethnicity.CHINESE}; Map<Ethnicity.Group, Long> result = Ethnicity.countByGroup(data);
方案选择建议
- 若必须严格实现
Ethnicity.WHITE.BRITISH的调用格式,优先选方案一,完美匹配需求,层次清晰。 - 若更看重代码简洁性和统计效率,方案二更合适,减少了嵌套层级,维护成本更低。
内容的提问来源于stack exchange,提问作者roboticcrow
相关产品推荐
相关产品推荐

