This article mainly studies the relevant content of EnumSet instead of bit domain in Java, as detailed below.
Reading notes "Effective Java Chinese Version 2"
Bit domain notation allows bit operations to be effectively performed such as union and intersection. But the bit domain has all the disadvantages of always illuminating the int enumeration, and even more. When the bitfield is printed in a digital form, it is much more difficult to translate a simple int enumeration constant. Even there is no easy way to traverse all elements represented by a bitfield.
//Bit field enumeration constant - OBSOLETEpublic class Test { public static final byte STYLE_BOLD = 1<<0; // 1 public static final byte STYLE_ITALIC = 1<<1; // 2 public static final byte STYLE_UNDERLINE = 1<<2; // 4 public static final byte STYLE_STRIKETHROUGH = 1<<3; // 6 //Parameter is bitwise OR of zero or more STYLE_ constants public void applyStyles(int styles) { ... }}The java.util package provides the EnumSet class to effectively represent multiple collections of multiple values extracted from a single enum type. This class implements the Set interface, providing rich functionality, type safety, and interoperability that can be obtained from any other Set implementation. However, in the internal specific implementation, each EnumSet content is represented as a bit vector. If the underlying enum type has 64 or fewer elements - most of them are. The entire EnumSet is represented by a single long, so its performance is better than the performance of the upper domain. Batch processing, such as removeAll and retainAll, are implemented using bit algorithms. Just like manually replacing bit domains. But you can avoid errors that are prone to manual operations and less elegant code, because EnumSet does this difficult task for you. `
//EnumSet - a modern replacement for bit fieldspublic class Text { public enum Style { BOLD, ITALIC, UNDERLINE, STRIKETHROUGH }; //Any Set could be passed in, but EnumSet is clearly best public void applyStyles(Set<Style> styles) { System.out.println(styles); } public void test() { applyStyles(EnumSet.of(Style.BOLD, Style.ITALIC)); }} Execute test() and output [BOLD, ITALIC] .
The EnumSet class combines the simplicity and performance advantages of bit domains and all the advantages of enum types, so there is no reason to use bit domain notation again. Unless it is a field that constitutes a communication protocol.
But in fact, EnumSet also has a disadvantage - it cannot create immutable EnumSet, but we can encapsulate the EnumSet with Collections.unmodifiableSet, but both simplicity and performance are affected.
public void test() { EnumSet<Style> styles = EnumSet.of(Style.BOLD, Style.ITALIC); Set<Style> unmodifiableStyle = Collections.unmodifiableSet(styles); unmodifiableStyle.add(Style.UNDERLINE);}In this way, if you add elements to the unmodifiableSet, you will report java.lang.UnsupportedOperationException
Summarize
The above is all the detailed explanation of EnumSet instead of bitfield code in Java. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!