What does EnumSet really mean?

后端 未结 10 1503
小蘑菇
小蘑菇 2020-12-04 16:08

I have the following example:

import java.util.EnumSet;
import java.util.Iterator;

public class SizeSet {

    public static void main(String[] args) {
             


        
相关标签:
10条回答
  • 2020-12-04 16:40
    EnumSet largeSize
    

    EnumSet largeSize is a set of Enum values which contains XL, XXL and XXXL.

    Size 
    

    Size is a class of Enum type with constant values S, M, L, XL, XXL, XXXL.

    largeSize.iterator()
    

    The Iterator for EnumSet is in natural order, the order in which the values of the enum were originally declared.

    • EnumSet class is a member of the Java Collections Framework & is not synchronized.
    • Its a high performance set implementation, they are much faster than HashSet.
    • All elements of each EnumSet instance must be elements of a single enum type.

    To Know more read the api doc : http://docs.oracle.com/javase/8/docs/api/java/util/EnumSet.html

    0 讨论(0)
  • 2020-12-04 16:41

    A simple Enum is a list of values that you can only select one from at a time. Using your example, a size can be only one of S, M, L, etc for any given cloth. You could use simple constants instead of the Enum but this has its advantages of readability, easier maintenance and strict type checking.

    An EnumSet will be used when you have the need for a variable to assume more than one Enum value at the same time. For instance, a font you write to screen with can be both bold and italic at the same time. An EnumSet will allow you to add the various values and to test whether one of those is actually set at any given time. If you have come to Java from other programming languages, this is the functionality usually called flags.

    Compare the two:

    enum Size { S, M, L, XL, XXL, XXXL }
    Size currentSize;
    ...
    currentSize = Size.S;
    ...
    if (currentSize == Size.S) ...
    

    defines, assigns and then checks for a single Enum value.

    enum FontStyle { Bold, Italic, Underline, Strikethrough }
    EnumSet<FontStyle> currentStyle;
    ...
    currentStyle = EnumSet.of(FontStyle.Bold, FontStyle.Italic);
    ...
    if (currentStyle.contains(FontStyle.Italic)) ...
    

    defines, assigns two Enum values at the same time, then checks whether one of those is actually set or not.

    0 讨论(0)
  • 2020-12-04 16:44

    Simplifying your code

    EnumSet<Size> largeSize = EnumSet.of(Size.XXXL, Size.XXL, Size.XL, Size.L);
    for(Size size: largeSize)
        System.out.print(size+" ");
    

    You can see that largeSize is a regular Set except its designed to store Enums. How is that different? Firstly the JVM knows all the possible values of the set which means instead of storing all the objects it can use a bitmap where 1 means the item is present and 0 means it is not. This also means the order of the set is the order of the ordinal values i.e. the order they were defined. This is why this prints

    L XL XXL XXXL
    

    If you want to know more detail I suggest you read the source for this class.

    0 讨论(0)
  • 2020-12-04 16:46

    As for any variable, its type is found in its declaration:

    EnumSet largeSize
    

    So yes, largeSize (which should be named largeSizes since it's a collection) is of type EnumSet. It should also be generified, and thus be declared as

    EnumSet<Size> largeSizes
    

    What it means, is that largeSizes is of type EnumSet. An EnumSet is a Set which contains enum instance of a specific enum type, in a more efficient way than other Set implementations (like HashSet, TreeSet, etc.). To know what an EnumSet is, read its API.

    0 讨论(0)
  • 2020-12-04 16:50

    Given the following example:

    ....
    public static final String S = "s";
    public static final String M = "m";
    public static final String L = "l";
    ....
    
    Set<String> sizeSet = new HashSet<String>();
    sizeSet.add(S);
    sizeSet.add(M);
    

    so, what is sizeSet in the above example?

    EnumSet is nothing different from the above example, only that EnumSet is a special Set implementation that works with and optimized with enum types, that's all.

    0 讨论(0)
  • 2020-12-04 16:51

    EnumSet is specifically used for storing the enum type element and iterating over it quickly. Eg.

    for (Day d : EnumSet.range(Day.MONDAY, Day.FRIDAY))
            System.out.println(d);
    

    The above snippet will display the days of the week from Monday through Friday.

    0 讨论(0)
提交回复
热议问题