Application of Java data type in real development two enumeration types

Source: Internet
Author: User

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)) requires a construction method with an 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's hibernate, it'll help me transform.

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 is de-compiled to see that a class is actually generated, which inherits the Java.lang.enum<e>, and the Enum is actually a class, except that the Java compiler helps us parse and compile the syntax.

The only difference is that enums implicitly inherit the enum, so it is not possible to inherit

= Enumtest.tue, ..... System.out.println ("Getdeclaringclass ():" +     test.getdeclaringclass (). GetName ()); //

Four: the application of Enumset,enummap

Refer to Java Enumeration usage

Java provides two tool classes--enumset and enummap that facilitate the operation of enums.

EnumSetis a high-performance implementation of enumeration types Set . It requires that the enumeration constants put into it must belong to the same enumeration type.
EnumMapis 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, So Enummap uses arrays to hold values that correspond to enumerated types. This makes the enummap highly efficient.

Application of Java data type in real development two enumeration types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.