JAVA 枚举类遍历与switch使用
Posted DylanZ
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA 枚举类遍历与switch使用相关的知识,希望对你有一定的参考价值。
1、定义枚举类
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.zxtech.framework.exception.UnknownEnumException;
import com.zxtech.framework.model.enums.IEnum;
import com.zxtech.framework.util.StringUtils;
/**
* <p>
* 流程节点名称枚举
* </p>
*
* @author Caratacus
*/
public enum InstanceNodeEnum implements IEnum {
JobsiteRequirementFeedbackMainProcess(0, "工地需求反馈主流程启动"),
JobsiteRequirementTurnback(1, "工地需求修改"),
FSDPICEngineerPretreatment(2,"FSD/PIC工程师预处理"),
InstallationOrServiceManagerApproval(3,"安装/维保经理审批"),
NonStandardDesign(4,"非标设计"),
PackageDesign(5,"包装设计"),
FSDPICManagerVerification(6,"FSD/PIC经理审核"),
ExportMaterialListforJDESARelease(7,"导出物料清单用于JDE/SA释放"),
PlanOrderConfirmationAMUIPO(8,"计划订单确认(AM/UI PO)"),
SupplierMaterialShippment(9,"供应商物料发运"),
MaterialConfirmation(10,"物料确认"),
OverseasMaterialQuotation(11,"海外物料报价"),
MaterialShippment(12,"物料发运");
@EnumValue
private final int value;
private final String desc;
InstanceNodeEnum(int value, String desc) {
this.value = value;
this.desc = desc;
}
@Override
public int getValue() {
return value;
}
public String getDesc() {
return desc;
}
/**
* 通过value取枚举
* * @param value
* * @return
*/
public static InstanceNodeEnum getTypeByValue(String value) {
if (StringUtils.isEmpty(value)) {
return null;
}
int valueKey = Integer.parseInt(value);
for (InstanceNodeEnum enums : InstanceNodeEnum.values()) {
if (enums.getValue() == valueKey) {
return enums;
}
}
return null;
}
/**
* 通过value取描述
* * @param value
* * @return
*/
public static String getDescByValue(int value) {
for (InstanceNodeEnum enums : InstanceNodeEnum.values()) {
if (enums.getValue() == value) {
return enums.getDesc();
}
}
return"";
}
}
2、枚举类遍历
for (InstanceNodeEnum nodeTypeEnum : InstanceNodeEnum.values()) {
nodeTypeEnum.getValue();
nodeTypeEnum.getDesc();
List<AMTTrackingTbFeedbackinfoDTO> curNodeList=amtTrackingInfoList.stream().filter(s-> (StringUtils.isNotEmpty(s.getInstanceNodeName())&&s.getInstanceNodeName().equals(nodeTypeEnum.getDesc()))).collect(Collectors.toList());
calcNodeAMTTrackingInfo(String.valueOf(nodeTypeEnum.getValue()),curNodeList,resultList);
}
3、枚举类结合switch使用
private void calcNodeAMTTrackingInfo(String nodeIndex,List<AMTTrackingTbFeedbackinfoDTO> curNodeList,List<JSONObject> resultList) {
String nowDate=DateUtils.getNowDate();
InstanceNodeEnum instanceNodeEnum = InstanceNodeEnum.getTypeByValue(nodeIndex);
switch (instanceNodeEnum) {
case JobsiteRequirementFeedbackMainProcess:
doSomething();
break;
case JobsiteRequirementTurnback:
doSomething();
break;
default:
break;
以上是关于JAVA 枚举类遍历与switch使用的主要内容,如果未能解决你的问题,请参考以下文章
Java--枚举类型(枚举类型实现接口 & 枚举类型之间的比较 & 枚举类型用于switch语句)