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; }
注:返回類型是枚舉