Java enumeration details and usage examples (covers all typical USES)

  • 2020-04-01 03:50:48
  • OfStack

In real programming, there are often "data sets" whose values are stable in the program and whose elements are finite.

For example, from Monday to Sunday, seven data elements make up the "data set" of the week, and four data elements make up the "data set" of the four seasons.

How best to use these "data sets" in Java? This is where enumerations come in handy, and the following code details their use.


package com.ljq.test;


public class TestEnum {
  
  public enum ColorEnum {
    red, green, yellow, blue;
  }
  
  
  public enum SeasonEnum {
    //Note: enumeration is written at the front, otherwise compilation errors occur
    spring, summer, autumn, winter;

    private final static String position = "test";

    public static SeasonEnum getSeason() {
      if ("test".equals(position))
        return spring;
      else
        return winter;
    }
  }
  
  
  public enum Gender{
    //Assignments are made through parentheses, and must be accompanied by a parameter constructor and a property and method, otherwise compilation fails
    //All or none of the values must be assigned, not part of them and part of them; If you do not assign a value, you cannot write a constructor, and the assignment compilation fails
    MAN("MAN"), WOMEN("WOMEN");
    
    private final String value;

    //The constructor can only be private by default, ensuring that the constructor can only be used internally
    Gender(String value) {
      this.value = value;
    }
    
    public String getValue() {
      return value;
    }
  }
  
  
  public enum OrderState {
    
    CANCEL {public String getName(){return " Has been cancelled ";}},
    
    WAITCONFIRM {public String getName(){return " To audit ";}},
    
    WAITPAYMENT {public String getName(){return " Waiting for payment ";}},
    
    ADMEASUREPRODUCT {public String getName(){return " Is distribution ";}},
    
    WAITDELIVER {public String getName(){return " Wait for the delivery ";}},
    
    DELIVERED {public String getName(){return " Has been shipped ";}},
    
    RECEIVED {public String getName(){return " Have the goods ";}};
    
    public abstract String getName();
  }
  
  public static void main(String[] args) {
    //Enumeration is a type used to define variables to limit their assignment; When assigning a value, the value in the enumeration is obtained by the enumeration name. Value
    ColorEnum colorEnum = ColorEnum.blue;
    switch (colorEnum) {
    case red:
      System.out.println("color is red");
      break;
    case green:
      System.out.println("color is green");
      break;
    case yellow:
      System.out.println("color is yellow");
      break;
    case blue:
      System.out.println("color is blue");
      break;
    }
    
    //Traverse the enumeration
    System.out.println(" traverse ColorEnum Values in enumeration ");
    for(ColorEnum color : ColorEnum.values()){
      System.out.println(color);
    }
    
    //Gets the number of enumerations
    System.out.println("ColorEnum The values in the enumeration are "+ColorEnum.values().length+" a ");
    
    //Gets the index location of the enumeration, starting at 0 by default
    System.out.println(ColorEnum.red.ordinal());//0
    System.out.println(ColorEnum.green.ordinal());//1
    System.out.println(ColorEnum.yellow.ordinal());//2
    System.out.println(ColorEnum.blue.ordinal());//3
    
    //Enumerated the default implementation of java.lang.Com parable interface
    System.out.println(ColorEnum.red.compareTo(ColorEnum.green));//-1
    
    //--------------------------
    System.out.println("===========");
    System.err.println(" Season for " + SeasonEnum.getSeason());
    
    
    //--------------
    System.out.println("===========");
    for(Gender gender : Gender.values()){
      System.out.println(gender.value);
    }
    
    //--------------
    System.out.println("===========");
    for(OrderState order : OrderState.values()){
      System.out.println(order.getName());
    }
  }
  
}


Related articles: