一、创建一个枚举对象
1、代码如下:
-
- import java.util.Arrays;
- import java.util.Iterator;
- import java.util.Optional;
-
- /**
- * Description: 异常类型枚举
- * @author w
- * @version 1.0
- * @date 2022/8/1 19:47
- */
- public enum ExpEnum {
-
- PWD_ERROR("5001","密码错误"),
-
- INNER_ERROR("5002","内部错误"),
-
- OTHER_ERROR("5005","其他错误");
-
- private String code ;
- private String msg ;
-
- ExpEnum(String code, String msg) {
- this.code = code;
- this.msg = msg;
- }
-
- public String getCode() {
- return code;
- }
-
- public void setCode(String code) {
- this.code = code;
- }
-
- public String getMsg() {
- return msg;
- }
-
- public void setMsg(String msg) {
- this.msg = msg;
- }
-
- /**@Description: 根据code获取枚举对象
- * @param code
- * @return
- * @version v1.0
- * @author wu
- * @date 2022/8/1 20:28
- */
- public static ExpEnum get(String code){
- Iterator
iterator = Arrays.stream(ExpEnum.values()).iterator(); - while (iterator.hasNext()){
- ExpEnum next = iterator.next();
- if(next.code.equals(code)){
- return next ;
- }
- }
- return null;
- }
-
- /**@Description: 根据code获取枚举对象 --- Java8方式
- * @param
- * @return
- * @version v1.0
- * @author wu
- * @date 2022/8/1 20:30
- */
- public static ExpEnum getByJava8(String code){
- Optional
first = Arrays.stream(ExpEnum.values()).filter(e -> e.code.equals(code)).findFirst(); - // ExpEnum expEnum = first.get();
- ExpEnum expEnum = first.orElse(null);
- return expEnum;
- }
- }
二、测试
1、遍历枚举、获取枚举对象的值
- /**@Description: 遍历枚举
- * @return
- * @version v1.0
- * @author wu
- * @date 2022/8/1 20:32
- */
- @Test
- public void forEachTest(){
- ExpEnum[] values = ExpEnum.values();
- for (ExpEnum e : values) {
- System.out.println(e.ordinal() + ",name :"+ e.name()+ ",code:"+e.getCode() +", msg:"+e.getMsg() );
- }
- }
-
- /**@Description: 获取枚举对象
- * @version v1.0
- * @author wu
- * @date 2022/8/1 20:35
- */
- @Test
- public void getTest(){
- ExpEnum expEnum = ExpEnum.get("5001");
- System.out.println(expEnum);
- }
-
- /**@Description: 获取枚举对象 --- Java8
- * @version v1.0
- * @author wu
- * @date 2022/8/1 20:37
- */
- @Test
- public void getByJava8Test(){
- ExpEnum byJava8 = ExpEnum.getByJava8("5002");
- System.out.println(byJava8);
- }
三、使用枚举简化多个if else
1、原代码情况
- public String process(String code){
- String res = null ;
- if("1".equals(code)){
- res = "状态一 if";
- }else if("2".equals(code)){
- res = "状态二 if";
- }else if("3".equals(code)){
- res = "状态三 if";
- }else if("4".equals(code)){
- res = "状态四 if";
- }
- return res;
- }
2、使用枚举Enum简化,代码如下:
- public String process2(String code){
- return Status.get(code).msg;
- }
-
- static enum Status {
-
- ONE("1","状态一"),
- TWO("2","状态二"),
- THREE("3","状态三"),
- FOUR("4","状态四");
-
- private String code ;
- private String msg;
-
- Status(String code, String msg) {
- this.code = code;
- this.msg = msg;
- }
-
- public String getCode() {
- return code;
- }
-
- public void setCode(String code) {
- this.code = code;
- }
-
- public String getMsg() {
- return msg;
- }
-
- public void setMsg(String msg) {
- this.msg = msg;
- }
-
- public static Status get(String code){
- Status status = Arrays.stream(Status.values()).filter(e -> e.getCode().equals(code)).findFirst().orElse(null);
- return status;
- }
- }