Emun Enumeration type Usage Analysis-including enumSet enumMap

Source: Internet
Author: User

Don't talk about anything. paste the Code directly ~ Record it for future use. Run the TestEnum class to view the effect ~ 1. enum Color [java]/*****/package hpf. test; import java. util. enumMap; import java. util. enumSet;/*** Enum class Color * @ author pengfei. hpf **/public enum Color implements ColorInterface {/*** Enumeration type definition, corresponds to the constructor */RED ("RED", 1), GREEN ("GREEN ", 2), BLANK ("white", 3), YELLOW ("YELLOW", 4); private String name; private int index; /** constructor * @ param name * @ param index */private Color (String name, int index) {this. name = name; this. index = index;}/* (non-Javadoc) * @ see hpf. test. colorInterface # getName () */@ Override public String getName () {return this. name;}/* (non-Javadoc) * @ see hpf. test. colorInterface # print () */@ Override public String print () {String msg = "index:" + index + "name:" + name; System. out. println (msg); return msg;} public static final EnumSet <Color> enumSet = EnumSet. allOf (Color. class); public static final EnumMap <Color, String> enumMap = new EnumMap <Color, String> (Color. class);} 2. interface Class [java]/*****/package hpf. test;/*** interface class * @ author pengfei. hpf **/public interface ColorInterface {/** get the name of the Color of the name Enumeration type * @ return */public String getName (); /** print information of the enumeration type * @ return */public String print ();} 3. test class [java] <pre name = "code" class = "java"> package hpf. test; import java. util. iterator;/** example of using the Enum class * @ author pengfei. hpf **/public class TestEnum {public static final int RED = 1; public static final int GREEN = 2; /*** @ param args */public static void main (String [] args) {enumFunc (); enumSetFunc (); enumMapFunc ();} /*** test the basic method */public static void enumFunc () {System. out. println ("enumFunc:" + Color. RED. getName (); Color. YELLOW. print ();}/*** all Enum objects in EnumSet are unique */public static void enumSetFunc () {Iterator <Color> it = Color. enumSet. iterator (); while (it. hasNext () {Color c = (Color) it. next (); c. print ();} System. out. println ("enumSet:" + Color. enumSet. toString ();}/*** enumMap test method. The key must be Enum-type data, and the value can be of any type */public static void enumMapFunc () {Color. enumMap. put (Color. RED, "This is RED"); Color. enumMap. put (Color. GREEN, "I am RED"); Color. enumMap. put (Color. YELLOW, "Oh, you are too yellow"); for (Color color: Color. enumMap. keySet () {System. out. println ("enumMap:" + Color. enumMap. get (color) ;}}</pre> [java]

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.