zoukankan      html  css  js  c++  java
  • [Java]遍历枚举类型为List

    Demo

    import com.google.common.collect.Lists;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public enum StandardOperationEntityType {
            CREATE("CODELIST", "数据字典"),
            DELETE("CODELIST_ITEM", "数据字典项");
    
            private final String code;
            private final String name;
    
        StandardOperationEntityType(String code, String name){
                this.code = code;
                this.name = name;
            }
    
            public static StandardOperationEntityType findByCode(String code) {
                for (StandardOperationEntityType type : values()) {
                    if (type.getCode().equals(code)) {
                        return type;
                    }
                }
                return null;
            }
    
            public static StandardOperationEntityType findByName(String name) {
                for (StandardOperationEntityType type : values()) {
                    if (type.getName().equals(name)) {
                        return type;
                    }
                }
                return null;
            }
    
            public String getCode() {
                return this.code;
            }
    
            public String getName() {
                return this.name;
            }
    
        public static List<Map<String, String>> toList() {
            List<Map<String, String>> list = Lists.newArrayList();//Lists.newArrayList()其实和new ArrayList()几乎一模
            for (StandardOperationEntityType item : StandardOperationEntityType.values()) {
                Map<String, String> map = new HashMap<String, String>();
                map.put("code", item.getCode());
                map.put("name", item.getName());
                list.add(map);
            }
            return list;
        }
    }
    

    X 参考文献

  • 相关阅读:
    LeetCode 79. 单词搜索
    LeetCode 1143. 最长公共子序列
    LeetCode 55. 跳跃游戏
    LeetCode 48. 旋转图像
    LeetCode 93. 复原 IP 地址
    LeetCode 456. 132模式
    LeetCode 341. 扁平化嵌套列表迭代器
    LeetCode 73. 矩阵置零
    LeetCode 47. 全排列 II
    LeetCode 46. 全排列
  • 原文地址:https://www.cnblogs.com/johnnyzen/p/13651003.html
Copyright © 2011-2022 走看看