Do application systems, we tend to assume that the user is white, so in order to ensure the normal system, we tend to limit the user’s parameters, and before and after the end of the user’s parameters to do verification. Can we prevent this in advance when designing? One way to do this is to use enum instead of int constants.

Benefits of enumeration:

Do application systems, we tend to assume that the user is white, so in order to ensure the normal system, we tend to limit the user’s parameters, and before and after the end of the user’s parameters to do verification. Can we prevent this in advance when designing? One way to do this is to use enum instead of int constants.

Benefits of enumeration:

1. Type safety

2. Convenience of use

Example 1

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class EnumDemo {     
    enum Color{     
        RED(3),BLUE(5),BLACK(8),YELLOW(13),GREEN(28);          
        private int colorValue;     
        private Color(int rv){     
         this.colorValue=rv;       
        }   
        private int getColorValue(){
            return colorValue;
        }   

        private int value(){
            return ordinal()+1; }}public static void main(String[] args) { 
        for(Color s : Color.values()) {     
            // values() returns an array.
            System.out.println(s.value()+":"+s.name()+"="+s.getColorValue());     
        }     
    }     
} 
</pre>
Copy the code

The output:

1:RED=3

2:BLUE=5

3:BLACK=8

4:YELLOW=13

5:GREEN=28

Among them

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">    /**
     * Returns the ordinal of this enumeration constant (its position
     * in its enum declaration, where the initial constant is assigned
     * an ordinal of zero).
     *
     * Most programmers will have no use for this method.  It is
     * designed for use by sophisticated enum-based data structures, such
     * as {@link java.util.EnumSet} and {@link java.util.EnumMap}.
     *
     * @return the ordinal of this enumeration constant
     */
    public final int ordinal() {
        return ordinal;
    }
</pre>
Copy the code

Example 2:

EnumMap is a Map implementation tailored specifically for enumeration types. While other Map implementations (such as HashMap) can do enumeration type instance-to-value mapping, using EnumMap is more efficient: It can only accept instances of the same enumeration type as key values, and because the number of instances of enumeration types is relatively fixed and limited, EnumMap uses arrays to hold values corresponding to enumeration types. This makes EnumMap very efficient.

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import java.util.*;
public enum Phase {
    SOLID, LIQUID, GAS;
    public enum Transition {
        MELT(SOLID, LIQUID), FREEZE(LIQUID, SOLID), BOIL(LIQUID, GAS), CONDENSE(
                GAS, LIQUID), SUBLIME(SOLID, GAS), DEPOSIT(GAS, SOLID);
        private final Phase src;
        private final Phase dst;
        Transition(Phase src, Phase dst) {
            this.src = src;
            this.dst = dst;
        }
        private static final Map<Phase, Map<Phase, Transition>> m = new EnumMap<Phase, Map<Phase, Transition>>(
                Phase.class);
        static {
            for (Phase p : Phase.values())
                m.put(p, new EnumMap<Phase, Transition>(Phase.class));
            for (Transition trans : Transition.values())
                m.get(trans.src).put(trans.dst, trans);
        }
        public static Transition from(Phase src, Phase dst) {
            returnm.get(src).get(dst); }}public static void main(String[] args) {
        for (Phase src : Phase.values())
            for (Phase dst : Phase.values())
                if(src ! = dst) System.out.printf("%s to %s : %s %n", src, dst,
                            Transition.from(src, dst));
    }
}
</pre>Copy the code