1.编写枚举类
假设枚举类如下,现在需要遍历里面的deptId
public enum AreaEnum { SHANDONG(1L, new BigDecimal(101527453), new BigDecimal(157900)), SHANGHAI(2L, new BigDecimal(24180000), new BigDecimal(6340.5)), JIANGXI(3L, new BigDecimal(45188635), new BigDecimal(166900)), SICHUAN(4L, new BigDecimal(83674866), new BigDecimal(486000)); private Long deptId; private BigDecimal people; private BigDecimal area; public Long getDeptId() { return deptId; } public BigDecimal getPeople() { return people; } public BigDecimal getArea() { return area; } AreaEnum(Long deptId, BigDecimal people, BigDecimal area) { this.deptId = deptId; this.people = people; this.area = area; } }
2.遍历枚举类
在枚举类里添加如下静态方法即可遍历
public static AreaEnum getByDeptId(Long deptId) { for (AreaEnum status : AreaEnum.values()) { if (status.getDeptId().equals(deptId)) { return status; } } return null; }
注:返回类型是枚举