Java record-81-EnumSet and EnumMap

Source: Internet
Author: User

Java record-81-EnumSet and EnumMap
The EnumSet name indicates its function. It is a new category added after J2SE5.0. It helps you create a set of enumerated values and provides a series of static methods, you can specify different set creation methods. The private Set implementation used by EnumSet and enumeration types. All keys in the enumeration set must come from a single Enumeration type, which is explicitly or implicitly specified when the set is created. EnumMap is a type designed for enumeration types, allowing you to easily use enumeration types and Map objects. Dedicated Map implementation used by EnumMap and enumeration type keys. All keys in the enumeration ing must come from a single Enumeration type, which is explicitly or implicitly specified when the ing is created. The enumeration ing is represented as an array internally.

enum FontConstant{    Plain, Bold, Italilc, Hello}public class EnumSetDemo {    public static  void showEnumSet(EnumSet<FontConstant> enumSet){        for(Iterator<FontConstant> iter = enumSet.iterator();iter.hasNext();){            System.out.println(iter.next());        }    }    public static void main(String[] args){        EnumSet<FontConstant> enumSet = EnumSet.of(FontConstant.Plain,FontConstant.Bold);        showEnumSet(enumSet); //Plain        Bold        System.out.println("---------------------");        showEnumSet(EnumSet.complementOf(enumSet)); //Italilc        Hello    }}

 

 
Enum Action {TURN_LEFT, TURN_RIGHT, SHOOT} public class EnumMapDemo {public static void main (String [] args) {Map <Action, String> map = new EnumMap <Action, string> (Action. class); map. put (Action. TURN_LEFT, "Turn right"); map. put (Action. TURN_RIGHT, "Turn left"); map. put (Action. SHOOT, "shooting"); for (Action action: Action. values () {System. out. println (map. get (action ));}}}

 

 

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.