• JAVA枚举


    key value 类(项目用不到的可以忽略这个类)

    import io.swagger.annotations.ApiModelProperty;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import lombok.experimental.Accessors;
    
    /**
     * key value 类
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Accessors(chain = true)
    public class KvVO {
        @ApiModelProperty(value = "key")
        private String k;
    
        @ApiModelProperty(value = "value")
        private String v;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    设备枚举:摄像头、流量计、压力计等

    enum 关键字 修饰
    直接上代码

    import cn.hutool.core.collection.ListUtil;
    import cn.hutool.core.text.CharSequenceUtil;
    import cn.hutool.core.util.ObjectUtil;
    import 上边那个类的包路径.KvVO;
    import lombok.Getter;
    
    import java.util.*;
    import java.util.stream.Collectors;
    
    /**
     * 设备枚举
     */
    @Getter
    public enum EquipmentEnum {
        _1(1, "摄像头"),
        _2(2, "流量计"),
        _3(3, "阀门"),
        _4(4, "压力计"),
        _5(5, "泄露报警器"),
        ;
    
        private Integer type;
        private String name;
    
        EquipmentEnum(Integer type, String name) {
            this.type = type;
            this.name = name;
        }
    
        public static String name(Integer type) {
            for (EquipmentEnum e : EquipmentEnum.values()) {
                if (ObjectUtil.equals(e.getType(), type)) {
                    return e.getName();
                }
            }
            return "";
        }
    
    	//和上边的 获取name 方法 得到的返回值一样
        public static String getNameByType(Integer type) {
            String getName = Arrays.stream(EquipmentEnum.values())
                    .filter(e -> Objects.equals(e.getType(), type))
                    .findFirst()
                    .map(EquipmentEnum::getName)
                    .orElse(CharSequenceUtil.EMPTY);
            return getName;
        }
    
        public static Integer type(String name) {
            for (EquipmentEnum e : EquipmentEnum.values()) {
                if (ObjectUtil.equals(e.getName(), name)) {
                    return e.getType();
                }
            }
            return 0;
        }
    
    	//和上边的 获取type 方法 得到的返回值一样
        public static Integer getTypeByName(String name) {
            Integer getType = Arrays.stream(EquipmentEnum.values())
                    .filter(e -> Objects.equals(e.getName(), name))
                    .findFirst()
                    .map(EquipmentEnum::getType)
                    .orElse(Calendar.ERA);
            return getType;
    
        }
    
    	//这里用到了 key value类
        public static List<KvVO> getKvVO() {
            List<KvVO> vo = Arrays.stream(EquipmentEnum.values())
                    .map(e -> new KvVO(e.getType().toString(), e.getName()))
                    .collect(Collectors.toList());
            return vo;
        }
    
        //移除 摄像头 和 阀门
        public static List<KvVO> getStationPipeKvVo() {
            ArrayList<Integer> integers = ListUtil.toList(EquipmentEnum._1.getType(), EquipmentEnum._3.getType());
    
            List<KvVO> vo = Arrays.stream(EquipmentEnum.values())
                    .filter(e -> !integers.contains(e.getType()))
                    .map(e -> new KvVO(e.getType().toString(), e.getName()))
                    .collect(Collectors.toList());
            return vo;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    接口BaseEnum
    public interface BaseEnum<T> {
        T getValue();
    
        default String getEnumName() {
            return null;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    GasObjectUtil
    import cn.hutool.core.util.ObjectUtil;
    
    import java.util.Arrays;
    
    /**
     * @author 孙永潮
     * @date 2024/01/03
     */
    public class GasObjectUtil extends ObjectUtil {
        public GasObjectUtil() {
        }
    
        public static boolean equals(Object obj1, Object... obj2) {
            boolean bool = Arrays.stream(obj2).anyMatch((e) -> {
                return equal(obj1, e);
            });
            return bool;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    枚举:唐山市各区县 AreaEnum
    package cn.包名.路径.enums;
    
    import cn.hutool.core.convert.Convert;
    import cn.hutool.core.util.ObjectUtil;
    import cn.hutool.core.util.StrUtil;
    import cn.tedu.util.GasObjectUtil;
    import lombok.Getter;
    import org.springframework.util.ObjectUtils;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.Map;
    import java.util.Objects;
    import java.util.stream.Collectors;
    
    /**
     * 枚举
     * 唐山市各区县
     */
    @Getter
    public enum AreaEnum implements BaseEnum<Integer> {
        _130203(130203, 1, "路北区", 1),
        _130202(130202, 2, "路南区", 2),
        _130204(130204, 3, "古冶区", 3),
        _130205(130205, 4, "开平区", 4),
        _130207(130207, 5, "丰南区", 5),
        _130208(130208, 6, "丰润区", 6),
        _130273(130273, 7, "高新开发区", 7),
        _130283(130283, 8, "迁安市", 8),
        _130281(130281, 9, "遵化市", 9),
        _130284(130284, 10, "滦州市", 10),
        _130224(130224, 11, "滦南县", 11),
        _130225(130225, 12, "乐亭县", 12),
        _130227(130227, 13, "迁西县", 13),
        _130229(130229, 14, "玉田县", 14),
        _130274(130274, 15, "海港开发区", 15),
        _130290(130290, 16, "国际旅游岛", 16),
        _130271(130271, 17, "芦台开发区", 17),
        _130272(130272, 18, "汉沽管理区", 18),
        _130209(130209, 19, "曹妃甸区", 19),
        //南堡 暂时属于 曹妃甸
        _20(130209, 20, "南堡开发区", 20),
        ;
    
        private final Integer region;
        private final Integer type;
        private final String name;
        private final Integer sort;
    
        public static final Map<Integer, String> AREA_MAP = Arrays.stream(AreaEnum.values())
                .collect(Collectors.toMap(AreaEnum::getType, AreaEnum::getName));
    
        public static final String mainAreaName = "主城区";
        public static final String allAreaName = "全市";
    
        AreaEnum(Integer region, Integer type, String name, Integer sort) {
            this.region = region;
            this.type = type;
            this.name = name;
            this.sort = sort;
        }
    
    	//用到了上边的 GasObjectUtil
        public static final List<String> mainArea = Arrays.stream(AreaEnum.values())
                .filter(e -> GasObjectUtil.equals(e, _130203, _130202, _130273))
                .map(AreaEnum::getType)
                .map(StrUtil::toString)
                .collect(Collectors.toList());
    
        public static final List<Integer> allArea = Arrays.stream(AreaEnum.values())
                .map(AreaEnum::getType)
                .collect(Collectors.toList());
    
        public static AreaEnum of(Integer type) {
            return Arrays.stream(AreaEnum.values())
                    .filter(e -> GasObjectUtil.equals(e.getType(), type))
                    .findFirst()
                    .orElse(null);
        }
    
        public static String name(String type) {
            return name(Convert.toInt(type));
        }
    
        public static String name(Integer type) {
            return Arrays.stream(AreaEnum.values())
                    .filter(e -> ObjectUtil.equals(e.getType(), type))
                    .findFirst()
                    .map(AreaEnum::getName)
                    .orElse(StrUtil.EMPTY);
        }
    
        public static Integer sort(String type) {
            return sort(Convert.toInt(type));
        }
    
        public static Integer sort(Integer type) {
            return Arrays.stream(AreaEnum.values())
                    .filter(e -> ObjectUtil.equals(e.getType(), type))
                    .findFirst()
                    .map(AreaEnum::getSort)
                    .orElse(0);
        }
    
        public static Integer type(String name) {
            return Arrays.stream(AreaEnum.values())
                    .filter(e -> ObjectUtil.equals(e.getName(), name))
                    .findFirst()
                    .map(AreaEnum::getType)
                    .orElse(null);
        }
    
        public static boolean isMain(String area) {
            return StrUtil.contains(area, ",");
        }
    
        public static String getAreaName(String area) {
            return isMain(area) ? AreaEnum.mainAreaName : name(area);
        }
    
        public static Integer getRegionByAreaType(String type) {
            return Arrays.stream(AreaEnum.values())
                    .filter(e -> Objects.equals(e.getType(), Integer.valueOf(type)))
                    .findFirst()
                    .map(AreaEnum::getRegion)
                    .orElse(null);
        }
    
        public Integer handleSort(String type) {
            return sort(type);
        }
        
        @Override
        public Integer getValue() {
            return this.type;
        }
    
        @Override
        public String getEnumName() {
            return this.name;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    获取Map的键值对

    正好用上边的枚举,写一个Map

    package cn.tedu.demo.service;
    
    import 上边的枚举.AreaEnum;
    
    import java.util.*;
    import java.util.stream.Collectors;
    
    /**
     * @author 孙永潮
     * @date 2024/01/03
     */
    public class MapDemo {
        public static void main(String[] args) {
            Map<Integer, String> map = AreaEnum.AREA_MAP;
    
            for (Map.Entry<Integer, String> entry : map.entrySet()) {
                //键值对的键
                Integer key = entry.getKey();
    
                //键值对的值
                String value = entry.getValue();
    
                System.out.println(key + " : " + value);
            }
    
            //所有的键
            Set<Integer> keys = map.keySet();
            for (Integer e : keys){
                System.out.print("     " + e);
            }
            System.out.println();
    
            //所有的值
            List<String> values = new ArrayList<>(map.values());
            for (String s : values){
                System.out.print(s + " ");
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
  • 相关阅读:
    TVS专业术语解读
    Go语言入门心法(十):Go语言操作MYSQL(CRUD)|事务处理
    转行软件测试两个多月,感觉很迷茫,下一步该如何提高自己?
    php反序列化靶场php-ser-lib-main 1-9关
    【分类器 Softmax-Classifier softmax数学原理与源码详解 深度学习 Pytorch笔记 B站刘二大人(8/10)】
    建筑能源管理(3)——建筑能源监管
    Qt在Windows上配置web assembly保姆教程
    mysql学习笔记
    python requests请求一个api接口报错
    异常exception和错误error即推荐用法@sneakythrow
  • 原文地址:https://blog.csdn.net/sungencheng/article/details/127773089