Java enumset_effective Java 2.0_item 1 knowledge point __java

Source: Internet
Author: User

Article Author: Tyan
Blog: noahsnail.com 1. Enumset

Enumset is a special implementation of the Java set interface, started in JDK 1.5, and the enum type is formally introduced into Java. Enumset has better performance than other sets that hold enumerated constants, and it is one of the best features in Java. Here are three ways to introduce Enumset,what,how,when. 2. What is Enumset

Enumset is an implementation of the set interface, which can only be used to store an enum constant or its subclasses, and cannot store other types. Enumset is a good example of creating an instance of a factory method in design mode.

Enumset is declared abstract class type, Enumset has two implementations, Regularenumset and Jumboenumset, but both implementations are package-private and cannot be accessed outside of the package. Therefore, you must use the factory method to create and return Enumset instances, which cannot be created through constructors. Enumset provides a variety of static factory methods for creating Enumset instances, such as the method of (function overload), copyof method, Noneof method, and so on. 3. How Enumset are implemented in Java

As has been said above, Enumset is an abstract class with two specific implementations: Java.util.RegularEnumSet and Java.util.JumboEnumSet. The main difference is that the former uses long to represent the number of elements, and the latter uses long[] to represent the number of elements. Both represent the number of elements using a bit-domain structure, that is, the number of elements by a long bits number, for example: Regularenumset uses a long to denote the number of elements, and a long value is represented by a 64-bit binary, so it can contain only the maximum number of elements 64, If the number of elements is greater than 64, Jumboenumset says, Jumboenumset long[] has a long value that can represent 64 elements, two long values can represent 128 elements, and so on. 4. When to use Enumset in Java

Item 32 in effective Java describes a Enumset usage scenario and is recommended for a look. When you need to make a specific grouping of enumerated types, you can use Enumset. For example, there are seven days in a week when you want to separate out the weekend (enum int pattern):

Private enum dayofweek{
     MONDAY, Tuesday, Wednesday, Thursday, FRIDAY, SATURDAY, SUNDAY;

Private Enumset weekend = Enumset.of (Saturday,sunday);
5. Important points about Enumset

Only one enumeration type can be included in a enumset.

A null element cannot be placed in the Enumset, which throws a null pointer exception.

Enumset is not a thread-safe.

The Enumset iterator is automatically fail-safe and weakly consistent, and does not throw concurrent modification exceptions, that is, the results of the modifications in the iteration process are not necessarily displayed during the iteration.

Enumset is a high-performance Java collection. Because it is array-based access, the time complexity of the Add,contains,next method is O (1).

Use Enumset instead of hashset when storing enumerated constants. 6. Source Code of Enumset

Enumset:

Public abstract class Enumset<e extends Enum<e>> extends abstractset<e> implements,
    java.io.Serializable {.....
        Enumset (Class<e>elementtype, enum<?>[] universe) {this.elementtype = ElementType;
    This.universe = Universe;
     }/** * Creates an empty enum set with the specified element type. * @param <E> The class of the elements in the set * @param elementtype the class object of the element t
     Ype for this enum * set * @return an empty enum set of the specified type. * @throws NullPointerException if <tt>elementType</tt> is null/public static <e extends Enum&lt ; E>> enumset<e> noneof (class<e> elementtype) {enum<?>[] universe = GetUniverse (elementType
        );

        if (universe = = null) throw new ClassCastException (ElementType + "not a enum"); if (Universe.length <=) return new Regularenumset<> (ElementType, Universe);
    else return to New jumboenumset<> (ElementType, Universe); }
    ...
}

Regularenumset:

Class Regularenumset<e extends enum<e>> extends enumset<e> {
    ...
    /**
     * Bit vector representation of this set.  The 2^k bit indicates the
     * presence of universe[k] in this set.
     *
    Private long elements = 0L;

    Regularenumset (Class<e>elementtype, enum<?>[] universe) {
        super (ElementType, Universe);
    }
    ...
}

Jumboenumset:

Class Jumboenumset<e extends enum<e>> extends enumset<e> {
    private static final long Serialversionuid = 334349849919042784L;

    /**
     * Bit vector representation of this set.  The ith bit of the jth
     * element of this array represents the  presence of universe[64*j +i]
     * in this set.
  
   */
    Private Long elements[];

    redundant-maintained for performance
    private int size = 0;

    Jumboenumset (Class<e>elementtype, enum<?>[] universe) {
        super (ElementType, universe);
        elements = new long[(universe.length +) >>> 6];
    }
    ...
}
  
7. Example of Enumset
Import Java.util.EnumSet;

Import Java.util.Set;
        public class Enumsetdemo {private enum Color {RED (255, 0, 0), GREEN (0, 255, 0), BLUE (0, 0, 255);
        private int R;
        private int g;
        private int B;
            Private Color (int r, int g, int b) {THIS.R = R;
            THIS.G = g;
        this.b = b;
        public int Getr () {return r;
        public int Getg () {return g;
        public int Getb () {return b; } public static void Main (String args[]) {//This'll draw line in yellow color enumset<c
        olor> yellow = Enumset.of (color.red, Color.green);
        DrawLine (yellow);
        RED + GREEN + BLUE = white enumset<color> white = Enumset.of (color.red, Color.green, Color.Blue);
        DrawLine (white);
        RED + BLUE = PINK enumset<color> PINK = Enumset.of (color.red, Color.Blue); DrawlinE (pink); public static void DrawLine (set<color> colors) {System.out.println ("requested colors to draw lines
        : "+ colors);
        for (color c:colors) {System.out.println ("drawing line in Color:" + C); 

}} output:requested Colors to draw lines: [RED, GREEN] drawing line in color:red drawing line in Color:green Requested Colors to draw lines: [RED, GREEN, BLUE] drawing line in color:red drawing line in Color:green drawing Li
 NE in Color:blue requested Colors to draw lines: [RED, BLUE] drawing line in color:red drawing line in Color:blue

Resources:

1, https://jaxenter.com/enumset-in-java-regularenumset-vs-jumboenumset-106051.html

2, effective Java 2.0 version of the item 1, this article is seen in the "effective Java" When you see the enumset, just want to carefully study the enumset. Item 32 tells about the Enumset usage scenario.

3, http://brokendreams.iteye.com/blog/2267485

4, http://javarevisited.blogspot.kr/2014/03/how-to-use-enumset-in-java-with-example.html

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.