Java basics-enumeration details, java basics Enumeration
Before JDK1.5, JAVA can define new types in two ways: classes and interfaces. For most object-oriented programming, it seems sufficient to have these two types, but it is not suitable in some special cases. For example, if you want to define a Color class, it can only have three types of Red, Green, and Blue, and other values are incorrect, the enumeration type will be introduced after JDK1.5.
EnumerationIt is actually a type similar to int and char. It defines the variable time limit and you can only assign the value specified in enum.
Public enum Color {RED, GREEN, BLUE; // defines three enumeration types };
There are three values in the enumeration. You can only retrieve the three values later.
Public class GetEnumContent {public static void main (String args []) {Color c = Color. BLUE; // retrieve the BLUE System. out. println (c );}};
Can be directly used on the Switch statement
Public class SwitchPrintEnum {public static void main (String args []) {for (Color c: Color. values () {// print (c);} public static void print (Color color) {switch (color) {case RED: {System. out. println ("red"); break;} case GREEN: {System. out. println ("green"); break;} case BLUE: {System. out. println ("blue"); break;} default: {System. out. println ("unknown color"); break ;}}}};
You can use the enum keyword to define an enumeration or an Eunm class.
Operation Method of enumeration class:
The Comparable interface has been implemented in enumeration, so the content in enumeration can be sorted.
Case 1
Import java. util. iterator; import java. util. set; import java. util. treeSet; enum Color {RED, GREEN, BLUE;} public class ComparableEnum {public static void main (String args []) {Set <Color> t = new TreeSet <Color> (); // Set the type t. add (Color. GREEN); // Add GREEN t. add (Color. RED); // Add the RED t. add (Color. BLUE); // Add the BLUE Iterator <Color> iter = t. iterator (); while (iter. hasNext () {System. out. print (iter. next () + ",");}}};
Case 2
Use EnumMap
Import java. util. enumMap; import java. util. map; enum Color {RED, GREEN, BLUE;} public class EnumMapDemo {public static void main (String args []) {Map <Color, String> desc = null; // define the Map object and specify the type desc = new EnumMap <Color, String> (Color. class); // instantiate the EnumMap object desc. put (Color. RED, "RED"); desc. put (Color. GREEN, "GREEN"); desc. put (Color. BLUE, "BLUE"); System. out. println ("====== output all content ======"); for (Color c: Color. values () {System. out. println (c. name () + "-->" + desc. get (c);} System. out. println ("======= output all key values ======"); for (Color c: desc. keySet () {System. out. print (c. name () + ",");} System. out. println (); System. out. println ("====== output all content ======"); for (String s: desc. values () {System. out. print (s + ",");}}};
Case 3
Use EnumSet
Import java. util. enumSet; enum Color {RED, GREEN, BLUE;} public class EnumSetDemo {public static void main (String args []) {EnumSet <Color> esOld = null; // declare an EnumSet object EnumSet <Color> esNew = null; System. out. println ("======= EnumSet. copyOf (Color. class) ==== "); esOld = EnumSet. noneOf (Color. class); // set all enumeration types to esOld in the EnumSet object. add (Color. RED); // Add content esOld. add (Color. GREEN); // Add esNew = EnumSet. copyOf (esOld); // copy from an existing set to print (esNew);} public static void print (EnumSet <Color> temp) {// special output operation for (Color c: temp) {// cyclically outputs the Content System in EnumSet. out. print (c + ",");} System. out. println ();}};