在项目中有许多常量,我们都是运用罗列(enum)来处置惩罚,下面我就和人人分享一个比较通用的代码
罗列
/** * 形貌: 常量范例 * / public enum ClientType { SYSTEM(0, "背景治理"), EDUCATION(1, "教育体系"), GOVERNMENT(2, "政府体系"); private Integer value; private String text; ClientType(Integer value, String text) { this.value = value; this.text = text; } public Integer getValue() { return this.value; } public String getText() { return this.text; } /** *依据值找相对应的中文 */ public static String getTextByValue(Integer value) { return Arrays.stream(values()) // java8新特征 -- stream流 .filter(x -> x.getValue().equals(value)) .map(ClientType::getText) .findFirst().orElse(""); } }
罗列在java代码运用比较简单
在应用层的运用方法
// 猎取范例相对应的数值 Integer type = ClientType .SYSTEM.getValue(); // 猎取中文 Intger code = 1; // 初始化 for (ClientType value : ClientType.values()) { if (type.value== code) { return type; // 差别的营业有差别的处置惩罚方式 } }
以上就是java 罗列运用方法的细致内容,更多请关注ki4网别的相干文章!