"Go" Java Basic notes – Introduction and static import of enumeration types-good

Source: Internet
Author: User
Tags compact

Original URL: http://www.itzhai.com/java-based-notes-introduction-and-use-of-an-enumeration-type-static-import.html#1.2 , use of the values method:

Java Basic Notes – Introduction to the use of enumeration types and static imports this article was posted by arthinking 4 years ago |Java Basics |  No Comments | Be onlookers 8,332 views+ 1, Enumeration (enum): 1.1, enumeration type two static methods: 1.2, the use of the values method: 1.2, the use of the values method: 1.3, create an enumeration constant containing the private member variable: 1.4, Enumeration type detailed description: 1.5, enumeration comparison: 1.6, Enumeration related methods: 2, enumset:2.1, of Method 2.2, Noneof Method 3, List Save enumeration type: 4, Enummap class: 5, enumeration in the actual development of use: 6, static import:
1. Enum (enum):

The enumeration type is added to JDK5.0 and is defined using the enum keyword, which can be defined as follows:

public enum weather{    Sunny,    Rainy,    Cloudy,}
1.1. Two static methods in Enum type:
Values ()        gets all enumerated constants of the enumerated type
valueOf (class<t> enumtype, String name) returns an enumeration constant of the specified enumeration type with the specified name.
1.2, the use of the values method:
For (Weather weather:Weather.values ()) {    System.out.println (Weather);} System.out.println (Weather.valueof (Weather.  "Sunny"));
1.2, the use of the values method:
For (Weather weather:Weather.values ()) {    System.out.println (Weather);} //The following output is SunnySystem.out.println (weather.valueof (Weather.") Sunny ")); 
1.3. Create an enumeration constant that contains the private member variable:
Public enum weather{Sunny ("Sunny"), Rainy ("Rainy Day"), Cloudy ("cloudy"); //Private member variable, save name private String value; Public String GetValue () { return value;} //With parameter constructor Weather (String value) { this.value = value;}} public static void Main (string[] args) {Weather weather1 = Weather.sunny; //The following output is "Sunny" System.out.println (Weather1.getvalue ());}           
1.4, enumeration type detailed description:

The enum keyword acts like a class or interface, essentially defining a category in which the implementation of the details is done by the compiler.

The custom enumeration type is essentially inherited from the Java.lang.Enum abstract class. Each member constant is, in fact, an instance of the enumerated type of its own definition, which is defined as final, all of which cannot be changed, and they are static,pulibc, namely:

Final enumeration constants;

We are no longer able to use this enumeration type to create new instances during run time, and these instances are fully determined during compilation.

1.5. Comparison of enumerations:
CompareTo (E o) compares the order of this enumeration with the specified object. Returns a negative integer, 0, or a positive integer, respectively, when the object is less than, equal to, or greater than the specified object. Enumeration constants can only be compared with other enumeration constants of the same enumeration type. The natural order in which this method is implemented is the order in which the constants are declared. Specified by: interface Comparable<e extends enum<e>> in the CompareTo parameter: O-the object to compare. Returns: A negative integer, 0, or a positive integer, depending on whether the object is less than, equal to, or greater than the specified object.
weather[] Arrays = weather.values (); For (Weather weather2:arrays) {    System.out.println ((weather.sunny). CompareTo (Weather2));}
1.6, enumeration of related methods:
Ordinalordinal() returns the ordinal of an enumeration constant (its position in the enumeration declaration, where the initial constant ordinal is 0). Most programmers do not use this method. It is designed for complex enumeration-based data structures such as Enumset and Enummap. Returns: The ordinal of an enumerated constant
for (Weather weather2:arrays) {    ":" + Weather2);}
2, Enumset:
enumset<e extends Enum<e>>extends abstractset<e>implements cloneable, Serializable

A private Set implementation that is used with the enumeration type. All keys in an enumeration set must come from a single enumeration type, which is explicitly or implicitly specified when the set is created. The enumeration set is internally represented as a bit vector. This representation is very compact and efficient. The spatial and temporal performance of this class should be good enough to be used as an alternative to the traditional int-based "bit sign", with the advantage of high quality and type safety. If its arguments are also an enumeration set, the bulk operations (such as Containsall and Retainall) should also run very fast.

This class helps us to create a collection of enumeration values, which provides a series of static methods that can be used to specify different sets of ways to build.

2.1. Of method
 of (E First,                                                E ... rest) creates an enumerated set that initially contains the specified element. The parameter list for this factory method uses the variable parameter feature, which can create an enumeration set that initially contains any element, but is likely to run slower than overloads that do not use variable arguments. Parameters: First-This set initially contains the element rest-this set is initially to contain the rest of the elements returned: The enumeration set that initially contains the specified element is thrown: nullpointerexception-If any argument is null, or rest is null
enumset<weather> Enumset = Enumset.of (Weather.sunny, Weather.rainy); For (iterator<weather> iter = Enumset.iterator (); Iter.hasnext ();) {    System.out.println (Iter.next ());}
2.2. Noneof method
noneof (class<e> ElementType) creates an empty enumeration set with the specified element type. Parameter: ElementType-The class object of this enumeration set element type is thrown: nullpointerexception-if ElementType is null
enumset<weather> EnumSet2 = enumset.noneof (Weather.  Class); Enumset2.add (Weather.sunny); Enumset2.add (Weather.rainy); For (iterator<weather> iter = Enumset2.iterator (); Iter.hasnext ();) {    System.out.println (Iter.next ());} 
3. List Save enumeration type:
New Arraylist<weather> (); List.add (Weather.sunny); List.add (Weather.cloudy); For (iterator<weather> iter = List.iterator (); Iter.hasnext ();) {    System.out.println (Iter.next ());}
4, Enummap class:
enummap<k extends enum<k>,v>        extends abstractmap<k,v> implements        Serializable, Cloneable

The private MAP implementation used with the Enum type key. All keys in an enumeration map must come from a single enumeration type, which is explicitly or implicitly specified when the mapping is created. An enumeration map internally represents an array. This representation is very compact and efficient.

Enumeration mappings are maintained according to the natural order of their keys, which is the order in which enumeration constants are declared. This is reflected in the iterator returned by the collection View (KeySet (), EntrySet (), and values ().

Enummap (class<k> KeyType) creates an empty enumeration map with the specified key type. Parameter: KeyType-This enumeration maps the class object of the key type thrown: NullPointerException-if KeyType is empty
New Enummap<weather, String> (Weather.   "Rainy Day");
5, enumeration in the actual development of the use of:
Static String getString (Weather Weather) {    if (Weather = = Weather.sunny) {        return Weather.Sunny.getValue ();    "non-conforming weather conditions";}  

Call the method above:

Weather Weather3 = Weather.sunny; System.out.println (getString (Weather3));
6. Static Import:

If you want to use a static variable or method, you must give the class where the method resides. Using static import allows all static and static methods of the imported class to be directly visible in the current class, so that they do not have to give their class names:

If you add the following import statement:

Static java.util.EnumSet.noneOf;

You can use this static method directly:

enumset<weather> EnumSet2 = noneof (Weather.  Class); Enumset2.add (Weather.sunny); Enumset2.add (Weather.rainy) ;
In addition to the article has special instructions, are it house original articles, reproduced please link the form to indicate the source.
This article links: http://www.itzhai.com/java-based-notes-introduction-and-use-of-an-enumeration-type-static-import.html keywords: Enum, Java, enumeration, static importarthinking Finger play guitar && technology More

"Go" Java Basic notes – Introduction and static import of enumeration types-good

Related Article

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.