Enumeration in Java -- EnumMap and EnumSet

Source: Internet
Author: User

To better support enumeration types, java. util adds two new classes: EnumMap and EnumSet. You can use them to operate enumeration types more efficiently. I will introduce you one by one:
EnumMap is a Map implementation tailored for enumeration types. Although other Map implementations (such as HashMap) can also Map enumeration instances to values, using EnumMap is more efficient: it can only receive instances of the same Enumeration type as key values, in addition, because the number of Enumeration type instances is relatively fixed and limited, EnumMap uses arrays to store values corresponding to the enumeration type. This makes EnumMap very efficient.
Tip: EnumMap uses ordinal () of the enumeration type internally to obtain the declared order of the current instance, and maintains the position of the value corresponding to the enumeration type instance in the array.
The following is a sample code using EnumMap. The enumerated DataBaseType contains all currently supported database types. For different databases, some database-related methods need to return different values. In this example, getURL is one.
// Currently supported Database Type Enumeration type definitions

Public enum DataBaseType {

MYSQL, ORACLE, DB2, SQLSERVER

}


// Obtain the database URL and the EnumMap Statement defined in a certain type.

......

Private EnumMap <DataBaseType, String> urls =

New EnumMap <DataBaseType, String> (DataBaseType. class );

Public DataBaseInfo (){

Urls. put (DataBaseType. DB2, "jdbc: db2: /// localhost: 5000/sample ");

Urls. put (DataBaseType. MYSQL, "jdbc: mysql: // localhost/mydb ");

Urls. put (DataBaseType. ORACLE, "jdbc: oracle: thin: @ localhost: 1521: sample ");

Urls. put (DataBaseType. SQLSERVER, "jdbc: microsoft: sqlserver: // localhost: 1433; DatabaseName = mydb ");

}


/**

* The corresponding URL is returned based on different database types.

* @ Param type new instance of the DataBaseType enumeration class

* @ Return

*/

Public String getURL (DataBaseType type ){

Return this. urls. get (type );

}
In actual use, the EnumMap object urls is usually filled by the code that is responsible for the initialization of the entire application. For the convenience of demonstration, the class fills in the content by itself.
As in the example, using EnumMap can easily bind enumeration types to different values in different environments. For example, if getURL is bound to a URL, it may be bound to a database driver in other code.
EnumSet is an enumeration type high-performance Set implementation. It requires that the enumerated constant to be put in it must belong to the same Enumeration type. EnumSet provides many factory methods for initialization, as shown in the following table:

EnumSet is implemented as the Set interface. It supports traversal of contained enumerated constants:
For (Operation op: EnumSet. range (Operation. PLUS, Operation. MULTIPLY )){

DoSomeThing (op );

}

Author: "Zuo Luo CTO"

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.