Common methods of Java Enum
@ (My first notebook) [Java, Enum]
Enumeration types are created using the enum keyword, implying that all types created are subclasses of the java.lang.Enum class (java.lang.Enum is an abstract class). Enumeration types conform to the generic schema Class Enum
Separately define enum classes, define enum classes in interfaces, and define enum classes in classes
Define the enum class separately.
public enum Color {
RED, GREEN, BLUE;
}
//Call: Color c = Color.RED
Define enum class in interface
public interface MyColor{
enum Color {
RED, GREEN, YELLOW;
}
}
Define enum classes in classes
public class myColor{
enum Color {
RED, GREEN, YELLOW;
}
}
Common operations such as traversal, switch, etc.
public class Test {
public static void main(String[] args) {
for (EnumTest e : EnumTest.values()) {
System.out.println(e.toString());
}
System.out.println("----------------I'm the dividing line.------------------");
EnumTest test = EnumTest.TUE;
switch (test) {
case MON:
System.out.println("Today is Monday");
break;
case TUE:
System.out.println("Today is Tuesday");
break;
// ... ...
default:
System.out.println(test);
break;
}
}
}
Adding variables and methods to enum
public enum State {
Normal("Normal state", 1), Update("Updated", 2), Deleted("Deleted", 3), Fired("Shielded", 4);
// Membership variables
private String name;
private int index;
// Constructor, note: Constructor cannot be public, because enum cannot be instantiated
private State(String name, int index) {
this.name = name;
this.index = index;
}
// General method
public static String getName(int index) {
for (State c : State .values()) {
if (c.getIndex() == index) {
return c.name;
}
}
return null;
}
// get set method
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}
Introduction to Common Methods of enum Objects
int compareTo(E o)
Compare this enumeration with the order of the specified object. Implemented the java.lang.Comparable interface, which returns the difference between the order of the two enumerated values, but the two enumerated values must belong to the same enumeration class
Class getDeclaringClass()
Returns a Class object corresponding to the enumeration type of this enumeration constant.
String name()
Returns the name of this enumeration constant and declares it in its enumeration declaration.
int ordinal()
Returns the ordinal number of the enumeration constant (its position in the enumeration declaration, where the initial constant ordinal number is zero).
String toString()
Returns the name of the enumeration constant, which is contained in the declaration.
valueOf is similar to toString
static
public class Test {
public static void main(String[] args) {
EnumTest test = EnumTest.TUE;
//compareTo(E o)
switch (test.compareTo(EnumTest.MON)) {
case -1:
System.out.println("TUE stay MON before");
break;
case 1:
System.out.println("TUE stay MON after");
break;
default:
System.out.println("TUE and MON In the same place");
break;
}
//getDeclaringClass()
System.out.println("getDeclaringClass(): " + test.getDeclaringClass().getName());
//name() and toString()
System.out.println("name(): " + test.name());
System.out.println("toString(): " + test.toString());
//ordinal(), the return value is from 0 start
System.out.println("ordinal(): " + test.ordinal());
}
}
//Output results:
TUE stay MON after
getDeclaringClass(): com.hmw.test.EnumTest
name(): TUE
toString(): TUE
ordinal(): 1
EnumSet, Application of EnumMap
public class Test {
public static void main(String[] args) {
// Use of EnumSet
EnumSet<EnumTest> weekSet = EnumSet.allOf(EnumTest.class);
for (EnumTest day : weekSet) {
System.out.println(day);
}
// Use of EnumMap
EnumMap<EnumTest, String> weekMap = new EnumMap(EnumTest.class);
weekMap.put(EnumTest.MON, "Monday");
weekMap.put(EnumTest.TUE, "Tuesday");
// ... ...
for (Iterator<Entry<EnumTest, String>> iter = weekMap.entrySet().iterator(); iter.hasNext();) {
Entry<EnumTest, String> entry = iter.next();
System.out.println(entry.getKey().name() + ":" + entry.getValue());
}
}
}