遍歷枚舉里的所有值


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;
    }

注:返回類型是枚舉

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM