Recently, there are often projects that need to set an int value for the enumeration value, and de-parse the int value to obtain the enumeration type. The code is as follows:
1 public enum MatchResultEnum { 2 3 /** 4 * Win 5 */ 6 WIN(0 ), 7 /** 8 * Lose 9 */ 10 LOSE(1 ), 11 /** 12 * Draw 13 */ 14 DRAW (2 ); 15 16 /** 17 * the code value of the match result 18 */ 19 private int code; 20 21 MatchResultEnum( int value) { 22 this.code = value; 23 } 24 25 public int getCode() { 26 return code; 27 } 28 29 30 public static MatchResultEnum parse(int value) { 31 MatchResultEnum[] values = values(); 32 for (MatchResultEnum matchResult : values) { 33 if (matchResult.code == value) { 34 return matchResult; 35 } 36 } 37 return null; 38 } 39 }