Enumeration
Wikipedia says "An enumeration of a collection of items is a complete, ordered listing of all of the items in that collection"
What is an Enumerated Type?
Enumerated Type is predefined set of constant values that form a group
This is available since JDK 1.5
Before the Enum was introduced, we can achieve similar thing by having a class with private constructor and public constant Object. Example shown below:
public class State { private State(); public static final State START = new State(); public static final State STOP= new State(); public static final State RUN= new State(); }
Enums in java 1.5
- All enums extends from java.lang.Enum
- Enum constants are by default public static
Example of Enum in JDK 1.5 and above:
enum State { START (1), STOP (2), RUN (3); private int level; private State(int level) { this.level = level; } }
1. use it in switch statements
switch(State) { case START: …………. case STOP: ………. case RUN: ……….. }
2. the enums are constants final.
- It can be compared with ==
- State threadState = State.RUN; threadState== State.RUN is always true
3. Enum can over ride methods
enum State { START (1) { @Override public void log() { System.out.println("--STARTING--"); } }, STOP (2) { @Override public void log() { System.out.println("--STOPPING--"); } }, RUN (3) { @Override public void log() { System.out.println("--RUNNING--"); } }; private int level; private State(int level) { this.level = level; } public abstract void log(); }
Importants methods of java.lang.enum
Returns the name
of this enum constant, exactly as declared in its enum declaration.
|
State.START.name()
returns "START" |
|
Returns the
ordinal of this enumeration constant (its position in its enum declaration,
where the initial constant is assigned an ordinal of zero).
|
State.START.ordinal()
returns 1
|
|
Returns the enum
constant of the specified enum type with the specified name.
|
State.valueof(State,
"START") returns State.START Object
|
|
T[] Values()
|
Fetch the list of
enum constants declared
|
State.values();
returns State[]
|
What is EnumSet?
This is a set implementation to be used in the enum types. All of the elements in an enum set must come from a single enum type that is specified,static EnumSet<state> set = EnumSet.range(STOP, RUN);
What is EnumMap?
This implements the Map Interface. It takes Enum as the keyComplete Example -
Download StateTester.javapackage test; import java.util.EnumMap; import java.util.EnumSet; public class StateTester { enum State { START (1) { @Override public void log() { System.out.println("--STARTING--"); } }, STOP (2) { @Override public void log() { System.out.println("--STOPPING--"); } }, RUN (3) { @Override public void log() { System.out.println("--RUNNING--"); } }; private int level; static EnumSet<State> set = EnumSet.range(STOP, RUN); private State(int level) { this.level = level; } public abstract void log(); } public static void main(String args[]) { System.out.println("Name : "+State.START.name()); // OUTPUT : START System.out.println("Ordinal : "+State.RUN.ordinal()); // OUTPUT : 2 System.out.println("valueOf: "+ State.valueOf("STOP")); // OUTPUT : STOP System.out.println("Enum.valueOf : "+ Enum.valueOf(State.class, "START")); // OUTPUT : START System.out.println("Enumset.contains :"+ State.set.contains(State.START)); //false System.out.println("Enumset.contains :"+ State.set.contains(State.STOP)); //true System.out.println("values : "+ State.values()); // array [Ltest.StateTester$State;@152b6651 EnumMap<State, String> stateEnum = new EnumMap<State,String>(State.class); stateEnum.put(State.RUN, "run"); System.out.println("ENUM MAP: "+stateEnum.get(State.RUN)); System.out.println("ENUM MAP null: "+stateEnum.get(State.START)); System.out.println("wrong valueOf : "+ State.valueOf("run")); // Exception in thread "main" java.lang.IllegalArgumentException: No enum const class test.StateTester$State.run } }
Output from the above java file:
Name : START Ordinal : 2 valueOf: STOP Enum.valueOf : START Enumset.contains :false Enumset.contains :true values : [Ltest.StateTester$State;@6bbc4459 ENUM MAP: run ENUM MAP null: null Exception in thread "main" java.lang.IllegalArgumentException: No enum const class test.StateTester$State.run at java.lang.Enum.valueOf(Enum.java:196) at test.StateTester$State.valueOf(StateTester.java:1) at test.StateTester.main(StateTester.java:65)
VW88
ReplyDeleteAgen Slot
Movie