• Java Enum 枚举用法 遍历枚举 获取枚举对象


    Java Enum 枚举用法 遍历枚举 获取枚举对象

    一、创建一个枚举对象

            1、代码如下:

    1. import java.util.Arrays;
    2. import java.util.Iterator;
    3. import java.util.Optional;
    4. /**
    5. * Description: 异常类型枚举
    6. * @author w
    7. * @version 1.0
    8. * @date 2022/8/1 19:47
    9. */
    10. public enum ExpEnum {
    11. PWD_ERROR("5001","密码错误"),
    12. INNER_ERROR("5002","内部错误"),
    13. OTHER_ERROR("5005","其他错误");
    14. private String code ;
    15. private String msg ;
    16. ExpEnum(String code, String msg) {
    17. this.code = code;
    18. this.msg = msg;
    19. }
    20. public String getCode() {
    21. return code;
    22. }
    23. public void setCode(String code) {
    24. this.code = code;
    25. }
    26. public String getMsg() {
    27. return msg;
    28. }
    29. public void setMsg(String msg) {
    30. this.msg = msg;
    31. }
    32. /**@Description: 根据code获取枚举对象
    33. * @param code
    34. * @return
    35. * @version v1.0
    36. * @author wu
    37. * @date 2022/8/1 20:28
    38. */
    39. public static ExpEnum get(String code){
    40. Iterator iterator = Arrays.stream(ExpEnum.values()).iterator();
    41. while (iterator.hasNext()){
    42. ExpEnum next = iterator.next();
    43. if(next.code.equals(code)){
    44. return next ;
    45. }
    46. }
    47. return null;
    48. }
    49. /**@Description: 根据code获取枚举对象 --- Java8方式
    50. * @param
    51. * @return
    52. * @version v1.0
    53. * @author wu
    54. * @date 2022/8/1 20:30
    55. */
    56. public static ExpEnum getByJava8(String code){
    57. Optional first = Arrays.stream(ExpEnum.values()).filter(e -> e.code.equals(code)).findFirst();
    58. // ExpEnum expEnum = first.get();
    59. ExpEnum expEnum = first.orElse(null);
    60. return expEnum;
    61. }
    62. }

    二、测试

            1、遍历枚举、获取枚举对象的值

    1. /**@Description: 遍历枚举
    2. * @return
    3. * @version v1.0
    4. * @author wu
    5. * @date 2022/8/1 20:32
    6. */
    7. @Test
    8. public void forEachTest(){
    9. ExpEnum[] values = ExpEnum.values();
    10. for (ExpEnum e : values) {
    11. System.out.println(e.ordinal() + ",name :"+ e.name()+ ",code:"+e.getCode() +", msg:"+e.getMsg() );
    12. }
    13. }
    14. /**@Description: 获取枚举对象
    15. * @version v1.0
    16. * @author wu
    17. * @date 2022/8/1 20:35
    18. */
    19. @Test
    20. public void getTest(){
    21. ExpEnum expEnum = ExpEnum.get("5001");
    22. System.out.println(expEnum);
    23. }
    24. /**@Description: 获取枚举对象 --- Java8
    25. * @version v1.0
    26. * @author wu
    27. * @date 2022/8/1 20:37
    28. */
    29. @Test
    30. public void getByJava8Test(){
    31. ExpEnum byJava8 = ExpEnum.getByJava8("5002");
    32. System.out.println(byJava8);
    33. }

    三、使用枚举简化多个if else        

            1、原代码情况

    1. public String process(String code){
    2. String res = null ;
    3. if("1".equals(code)){
    4. res = "状态一 if";
    5. }else if("2".equals(code)){
    6. res = "状态二 if";
    7. }else if("3".equals(code)){
    8. res = "状态三 if";
    9. }else if("4".equals(code)){
    10. res = "状态四 if";
    11. }
    12. return res;
    13. }

            

            2、使用枚举Enum简化,代码如下:

    1. public String process2(String code){
    2. return Status.get(code).msg;
    3. }
    4. static enum Status {
    5. ONE("1","状态一"),
    6. TWO("2","状态二"),
    7. THREE("3","状态三"),
    8. FOUR("4","状态四");
    9. private String code ;
    10. private String msg;
    11. Status(String code, String msg) {
    12. this.code = code;
    13. this.msg = msg;
    14. }
    15. public String getCode() {
    16. return code;
    17. }
    18. public void setCode(String code) {
    19. this.code = code;
    20. }
    21. public String getMsg() {
    22. return msg;
    23. }
    24. public void setMsg(String msg) {
    25. this.msg = msg;
    26. }
    27. public static Status get(String code){
    28. Status status = Arrays.stream(Status.values()).filter(e -> e.getCode().equals(code)).findFirst().orElse(null);
    29. return status;
    30. }
    31. }

            

  • 相关阅读:
    一个信号间相互干扰问题的发现及解决方法
    UDP程序设计
    【无标题】软件测试自动化“领导者”SmartBear举办首场中国线上研讨会:洞悉全球自动化测试走向,探讨降本增效之策
    java基础1
    猿创征文|基于SSM框架+java+maven+jsp的小项目
    linux学习笔记
    springBoot组件注册
    MySQL的
    DTC(diagnostic trouble code)
    新营销模式之分享购营销模式~你见过这种营销模式吗?
  • 原文地址:https://blog.csdn.net/HaHa_Sir/article/details/126109331