Java Basics---Enumeration

Source: Internet
Author: User

Before JDK1.5, Java can have two ways of defining new types: Classes and interfaces, and for most object-oriented programming, these two seem sufficient, but in some special cases it is inappropriate. For example, to define a color class, it can only have red,green,blue three, other values are errors, and an enumeration type is introduced after JDK1.5.

Enumeration is actually a type, similar to int, Char, which is the limit input when defining variables, you can only assign the values specified in enum.

public enum color{    Red,green,blue;    Defines the type of three enumerations};

There are three values in the enumeration, which can only be taken from these three items at a later time.

public class getenumcontent{public    static void Main (String args[]) {        Color c = color.blue;        Remove the blue        System.out.println (c);    }};

Can be used directly on the switch statement

public class switchprintenum{public  static void Main (String args[]) {for    (Color c:color.values ()) {// Output the entire contents of the enumeration      print (c);}  }  public static void print (color color) {    switch (color) {case      red:{        System.out.println ("Red Color");        break;      }      Case green:{        System.out.println ("green color");        break;      }      Case blue:{        System.out.println ("Blue Color");        break;      }      default:{        System.out.println ("Unknown Color");        Break;}}}  ;

You can define an enumeration by using the Enum keyword, or you can define a EUNM class

How to enumerate classes:

The enumeration already implements the comparable interface, so the content in the enumeration can be sorted.

Case One

Import java.util.iterator;import java.util.Set; import java.util.TreeSet; enum color{  Red,green,blue;} public class comparableenum{public  static void Main (String args[]) {    set<color> t = new treeset<color& gt; ();//set Type    t.add (color.green);//Add Green    t.add (color.red);//Add Red    t.add (Color.Blue);//Add Blue    Iterator<color> iter = T.iterator ();    while (Iter.hasnext ()) {      System.out.print (Iter.next () + ",");}}  };

Case listing Two

Using Enummap

Import java.util.EnumMap; import java.util.Map; enum color{  RED, GREEN, BLUE;} public class enummapdemo{public  static void Main (String args[]) {    map<color,string> desc = null;//define Map Object , specifying the type    desc = new Enummap<color,string> (color.class);//Instantiate Enummap object    desc.put (color.red, "Red");    Desc.put (Color.green, "green");    Desc.put (Color.Blue, "Blue");    System.out.println ("====== Output All content ======");    For (Color c:color.values ()) {      System.out.println (c.name () + "-" + Desc.get (c));    }    System.out.println ("====== output all key value ======");    For (Color C:desc.keyset ()) {      System.out.print (c.name () + ",");    }    System.out.println ();    System.out.println ("====== Output All content ======");    For (String s:desc.values ()) {      System.out.print (S + ",");}}}  ;

Case listing Three

Using Enumset

Import Java.util.EnumSet; enum color{  RED, GREEN, BLUE;} public class enumsetdemo{public  static void Main (String args[]) {    enumset<color> esold = null;//declaration of a enums Et object    enumset<color> esnew = null;    System.out.println ("======== enumset.copyof (Color.class) =====");    Esold = enumset.noneof (Color.class);//Set all types of enumerations into Enumset objects    Esold.add (color.red);//Add Content    Esold.add ( Color.green);//add content    esnew = enumset.copyof (esold);//Copy from existing set    print (esnew);  }  public static void print (Enumset<color> temp) {//Dedicated output action    for (Color c:temp) {///  loop output Enumset contents      System.out.print (c + ",");    }    System.out.println ();  }};
    • This article is from: Linux Learning Tutorial Network

Java Basics---Enumeration

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.