java enum枚举转list和Map 原创 2021-06-10 15:08:28.0 阅读(9875)次 java中如何将枚举的定义转成list或者Map,让我们更好的操作枚举值? 思路是使用枚举的values()方法,将枚举定义转成对应你想要的数据类型。 分享代码如下,其中AlarmType中定义了很多枚举值: ```java public class EnumToListAndMap { public enum AlarmType{ OVER_SPEEDING_ALARM("overSpeedingAlarm","超速行驶预警"), DEVICE_IDLE_ALARM("deviceIdleAlarm","设备空闲预警"), UNLOCK_ALARM("unlockAlarm","设备未上锁预警"); private String code; private String remark; private AlarmType(String code,String remark) { this.code = code; this.remark = remark; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } /** * 根据code取枚举类型 * @param code * @return */ public static AlarmType findByCode(String code) { for (AlarmType alarmType : values()) { if (alarmType.getCode().equals(code)) { return alarmType; } } return null; } /** * 获得所有枚举类型到list * @return */ public static List getAllToList() { List list = new ArrayList<>(); AlarmType[] values = values(); Collections.addAll(list, values); return list; } /** * 获得所有枚举类型到map * @return */ public static Map getAllToMap() { Map map = new HashMap<>(); for (AlarmType alarmType : values()) { map.put(alarmType.getCode(), alarmType); } return map; } } public static void main(String[] args) { System.out.println(AlarmType.findByCode("unlockAlarm").getRemark()); System.out.println("----------"); List list = AlarmType.getAllToList(); for (AlarmType alarmType : list) { System.out.println(alarmType.getCode()); } System.out.println("----------"); Map map = AlarmType.getAllToMap(); for (String key : map.keySet()) { System.out.println(map.get(key).getCode()); } } } ``` 上面代码输出结果如下: ``` 设备未上锁预警 ---------- overSpeedingAlarm deviceIdleAlarm unlockAlarm ---------- unlockAlarm deviceIdleAlarm overSpeedingAlarm ``` java 上一篇:如何清空或初始化quartz定时相关表时报外键引用错误 下一篇:fastjson转字符串时保留null空字段
相关文章 Jep实现Java表达式计算(2057) 阿里巴巴p3c代码规范eclipse插件(1554) java使用snakeyaml库读取和操作yml文件(2496) java通过域名获取域名的IP地址(1520) jmeter随机日期参数(1128) 线程安全测试 ArrayList Collections.synchronizedList CopyOnWriteArrayList(1514) ThreadLocal正确使用姿势,避免再掉坑(1214) java解析request或url中的参数工具类代码(2919) Base64编码出现换行符(2606) Java实现Https发送get和post请求(2738) 推荐文章 使用spring4实现websocket连接(1) Parameter index out of range (1 > number of parameters, which is 0(7) spring cloud+feign+mybatis中使用seata0.9实现分布式事务(7) spring cloud gateway报错Only one connection receive subscriber allowed(82) spring cloud中Feign调用诡异报错MethodNotAllowed: status 405 reading(116) elasticsearch7.1保存时报错: Validation Failed: 1: type is missing;(7) 聊聊数据保存到MySQL后数据乱码的问题(1) jquery对象与dom对象互转(1) linux使用epel源yum安装iftop、nload、nginx等(2) linux下nginx安装其他模块(1) 热门文章 java stream去重的几种方式(40819) the dependencies of some of the beans in the application context form a cycle(16324) 解决mybatis打印查询结果集造成太多日志的问题(9839) java enum枚举转list和Map(9722) java stream List转Map与List转List与Map转List以及List转Map(8468) ServletRequest转HttpServletRequest设置header之后取不到header的问题(8434) java中BufferedImage转成 base64字符串(8098) 切分List集合为多个List集合(7385) maven修改jar包版本不生效解决办法(6990) bootstrap.yml配置报错:Could not resolve placeholder 'xx' in value (6949) 标签列表 java java java java java java java基础 微服务 异常处理 mysql clickhouse clickhouse clickhouse clickhouse clickhouse spring cloud spring boot linux elasticsearch feign jdbc spring js docker postgresql solr seata nginx maven gateway hsqldb 数据库 架构 大数据分析 分布式事务 redis canal dubbo hadoop 消息队列 win10 websocket springmvc git html select2 mybatis jenkins rocketmq quartz activemq 数据库集群 ajax bat 电脑 笔记 eclipse 设计模式 阿里云 github freemarker jvm jquery javamail redission redission对象 hystrix http hibernate springmail svn ubuntu ueditor xheditor zookeeper 分布式 小程序 开发工具 gitlab