What does EnumSet mean?

I have the following example:

import java.util.EnumSet; import java.util.Iterator; public class SizeSet { public static void main(String[] args) { EnumSet largeSize = EnumSet.of(Size.XL,Size.XXL,Size.XXXL); for(Iterator it = largeSize.iterator();it.hasNext();){ Size size = (Size)it.next(); System.out.println(size); } } } enum Size { S, M, L, XL, XXL, XXXL; } 

In this code, I can understand that Enum creates an Enum size type.

My question is: is there a largeSize object of type EnumSet? What does this really mean? I really want to understand this better.

+65
java object enums enumset
Aug 6 2018-12-12T00:
source share
11 answers

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

 EnumSet largeSize 

So yes, largeSize (which should be called largeSizes since it is a collection) is of type EnumSet . It must also be generalized and thus declared as

 EnumSet<Size> largeSizes 

This means that largeSizes is of type EnumSet . EnumSet is a Set that contains an enum instance of a specific enumeration type in a more efficient way than other implementations of Set (for example, HashSet , TreeSet , etc.). To find out what EnumSet , read its API .

+42
Aug 6 2018-12-12T00:
source share

Code simplification

 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 that it is designed to hold Enums. How is everything different? Firstly, the JVM knows all the possible values ​​of the set, which means instead of storing all the objects that it can use a bitmap, where 1 means that the element is present, and 0 means that it is not. It also means that the order of the set is the order of the ordinal values, that is, the order they determined. That's why it prints

 L XL XXL XXXL 

If you want to know in more detail, I suggest you read the source of this class.

+38
Aug 06 2018-12-12T00:
source share

A simple Enum is a list of values ​​that you can select only once at a time. Using your example, size can only be one of S, M, L, etc. For any fabric. You can use simple constants instead of Enum , but this has its advantages of readability, ease of maintenance, and strong type checking.

An EnumSet will be used when you need a variable to accept more than one Enum value at the same time. For example, the font you write on the screen can be bold and italic at the same time. EnumSet allows you to add various values ​​and check whether one of them is set at any given time. If you came to Java from other programming languages, this is a feature commonly called flags .

Compare two:

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

detects, assigns, and then checks 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)) ... 

determines whether it assigns two Enum values ​​at the same time, and then checks whether one of them is actually set.

+37
Jan 03 '15 at 12:57
source share

From Joshua Bloch, the book itself and in its own word:

The java.util package provides the EnumSet class for efficiently representing sets of values ​​taken from one type of enumeration. This class implements the Set interface, which provides all the richness, type of security, and interoperability that you get with any other Set implementation. But internally, each EnumSet is represented as a bit vector. If the base enumeration type has sixty-four or fewer elements - and most of them - the entire EnumSet is represented by one long, so its performance is comparable to the characteristics of a bit field. Bulk operations such as removeAll and keepAll are implemented using bit-wise arithmetic, just as you will do manually for bit fields. But you are isolated from the ugliness and error when using the manual bit spit. EnumSet does the hard work for you.

So, we can do something like this.

 public class SizeSet { public enum Size {S, M, L, XL, XXL, XXXL} public void applySize(Set<Size> sizes){} } 

The client code calling it might do something like

 SizeSet largeSizeSet = new SizeSet(); largeSizeSet.applySize(EnumSet.of(Size.L, Size.XXL, Size.XXL)); 

Note that the applySize method accepts Set<Size> , not EnumSet<Size> . Although this is fairly obvious, and the likelihood that the client passes EnumSet to this method is a good practice to accept an interface rather than a specific implementation.

+12
Aug 16 '16 at 2:04 on
source share

EnumSet is specifically used to store an element of type enum and quickly repeat it. For example.

 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 to Friday.

+7
Jan 09 '15 at 8:43
source share

According to Oracle doc

Enum sets are represented internally as bit vectors. This view is extremely compact and efficient.

Readable, safe type, low memory ... What do you need more?

+4
Feb 04 '15 at 18:58
source share

In 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 no different from the above example, only EnumSet is a special implementation of Set that works and is optimized with enumeration types, that's all.

+3
Aug 6 '12 at 9:15
source share
 EnumSet largeSize 

EnumSet largeSize is a set of Enum values ​​containing XL, XXL, and XXXL.

 Size 

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

 largeSize.iterator() 

The iterator for EnumSet is in natural order, the order in which the enumeration values ​​were originally declared.

  • The EnumSet class is a member of the Java collection structure and is not synchronized.
  • Its implementation with high performance, they are much faster than HashSet.
  • All elements of each EnumSet instance must be elements of a single enumeration type.

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

+3
Aug 10 '16 at 13:12
source share

EnumSet is a specialized collection of Set for working with enum classes. It implements the Set interface and extends from AbstractSet :

enter image description here

When you plan to use EnumSet, you should take into account some points:

  1. It can contain only enumeration values, and all values ​​must belong to the same enumeration
  2. This prevents null values ​​from being thrown by throwing a NullPointerException in an attempt to do so.
  3. It is not thread safe, so we must synchronize it externally if necessary
  4. Items are stored in the order in which they are declared in the enumeration.
  5. It uses a failover iterator that works with the copy, so it will not throw a ConcurrentModificationException if the collection changes when iterating over it.

Now, the EnumSet largeSize part is a variable declaration, so yes, it is an EnumSet type. Note that you can add an item type to the collection for more type safety: EnumSet<Size>

Check out this article to learn more about EnumSet .

0
Jun 15 '19 at 10:16
source share

Typically, EnumSet always preferable to any other Set implementation when we store enum values.

Benefits of using EnumSet :

Methods in EnumSet implemented using arithmetic bitwise operations. These calculations are very fast, and therefore all basic operations are performed in constant time.

If we compare EnumSet with other implementations of Set, such as HashSet , the former is usually faster because the values ​​are stored in a predictable order and only one bit needs to be checked for each calculation. Unlike a HashSet, there is no need to calculate a hash code to find the correct segment.

Moreover, due to the nature of bit vectors, EnumSet very compact and efficient. Therefore, it uses less memory with all its attendant benefits.

The rules of sonarsors say :

When all elements in a set are values ​​from the same enumeration, the set can be replaced by the EnumSet, which can be much more efficient than other sets, since the basic data structure is a simple bitmap.

Enummap

Source here

0
Aug 28 '19 at 12:42
source share

It quickly turns all Enum elements into a set; EnumSet is another type of Set.

 public enum Style { BOLD, ITALIC, UNDERLINE, STRIKETHROUGH } EnumSet.of(Style.BOLD, Style.ITALIC); 
-2
Jul 02 '13 at 12:50
source share



All Articles