In actual programming, there are often such "datasets", whose values are stable in the program, and the elements in the "dataset" are limited. Before JDK1.5, people used interfaces to describe this type of data.
1.5 Introducing enumerations After
One: The enumeration class basic syntax definition:
To create an enumeration type to use the enum keyword, if you want to declare a simple enumeration, the attributes are separated by commas, and if it is an attribute with an ID, similar (male (1)) This also requires a construction method with ID
Simple Enumeration class:
Public enum Colorenum { red, green, yellow, blue;}
Enumerations can add properties and methods like normal classes, as follows
Public enum Gender { MALE (// male / female int ID; Gender (int ID) { this. id = ID; } Public int getId () { return ID; }}
If you do not add a constructor, directly after the "type Plus (1)" This form will error, as follows:
enum Colorenum { red (1), Green (2), yellow (3), Blue (4);//Compile Error: the constructor colorenum (int) is undefined}< /c3>
Because the nature of an enum class is an enum class, it can add a number of custom methods: for example, the top GetID ()
Use:
Can be used to foreach,switch incoming enumeration variables, case is each type defined within the enumeration.
Gender Gender = gender.womenswitch (Gender) { case male: System.out.println ("male"); Break ; Case female: System.out.println ("female"); break;
int
compareTo(E o)
Compares this enumeration with the order of the specified objects.
Class<E>
getDeclaringClass()
Returns the Class object that corresponds to the enumeration type of this enumeration constant.
String
name()
Returns the name of this enumeration constant, declared in its enumeration declaration.
int
ordinal()
Returns the ordinal of an enumeration constant (its position in the enumeration declaration , where the initial constant ordinal is zero ).
String
toString()
Returns the name of the enumeration constant, which is contained in the Declaration.
static <T extends Enum<T>> T
valueOf(Class<T> enumType, String name)
Returns an enumeration constant of the specified enumeration type with the specified name .
Two: Application enumeration in Web projects
@Enumerated Private Gender Gender;//entity Class
Let's take a look at the gender configuration.
enum Gender { MALE (// male / female int ID; Gender (int ID) { this. id = ID; } Public int getId () { return ID; }}
If the original DAO of the soundtrack is called, the result of the query is the map
Uservo.setgender (gender.valueof (String) map.get ("Gender"));//If the query is a map, then gender.valueof (method to get the desired enumeration
If it is hibernate, then we do not need to go through the relationship between the query is what, just need to guide the enumeration type can be
New Arraylist<uservo>(); Try { = "SELECT * from the User U left join u.institutions INS where INS.institution.id = '" + ID + "' Order by U.code "; = Userdao.query (queryString);//If you call Hibernate, the query does not need to call the valueof () method if null && userlist.size () > 0) { }}
Three: Principle Analysis
Refer to the Java Enum (enumeration) using a detailed + summary, thank you very much
The syntax structure of an enum is not the same as the syntax of class, but a class file is generated after the compiler compiles. The class file has been de-compiled in effect to generate a category that inherits the java.lang.enum<e>. So, the enum is essentially a Java compiler that helps us do a common class of parsing and compiling grammars.
The only difference is that the enum implicitly inherits the enum, so it cannot be inherited :
= Enumtest.tue, ..... System.out.println ("Getdeclaringclass ():" + test.getdeclaringclass (). GetName ()); //
Four: the application of Enumset,enummap
Enumset and Enummap are the more important uses of enumerations. They are tool classes that manipulate enumeration objects. Specific details recommended to take a look at the Java enumeration usage in detail, here, I briefly summarize:
EnumSet
is a high-performance implementation of enumeration types Set
. It requires that the enumeration constants put into it must belong to the same enumeration type.
EnumMap
is an implementation that is specifically tailored for enumeration types Map
. Although using other map implementations (such as HashMap) can also complete enumeration type instances to be worth mapping, using ENUMMAP is more efficient: it can only receive instances of the same enumeration type as key values, and because the number of enumerated type instances is relatively fixed and limited, Enummap uses an array to hold the values corresponding to the enumeration type . This makes the Enummap highly efficient .
/the use of Enumset System.out.println ("Enumset Show"); Enumset<ErrorCodeEn> Errset = Enumset.allof (errorcodeen.class); for(Errorcodeen e:errset) {System.out.println (E.name ()+ " : " +e.ordinal ());}//Use of EnummapSystem.out.println ("Enummap Show"); Enummap<statemachine.signal, string> errmap =NewEnummap (statemachine.signal.class); Errmap.put (StateMachine.Signal.RED,Red light); Errmap.put (StateMachine.Signal.YELLOW,"Yellow Light"); Errmap.put (StateMachine.Signal.GREEN,Green light); for(iterator<map.entry<statemachine.signal, string>> iter =Errmap.entryset (). iterator (); Iter.hasnext ();) {Map.entry<statemachine.signal, string> entry =Iter.next (); System.out.println (Entry.getkey (). Name ()+ " : " +Entry.getvalue ());}
Java data types apply two enumerations in real-world development