Java EnumSet replaces bit domain code details, javaenumset details
This article focuses on the Content of replacing bit fields with EnumSet in Java.
Reading NotesTive Java Chinese Version 2nd
Bit domain notation allows the use of bit operations to effectively execute a set operation such as union and intersection. However, bitwise fields have all the shortcomings of int enumeration, and even more. When the bit field is printed as a number, it is much more difficult to translate the bit field than to translate a simple int enumerated constant. Even, it is not easy to traverse all elements represented by bit fields.
//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 sets of multiple values extracted from a single Enumeration type. This class implements the Set interface, which provides rich functions, type security, and the interoperability that can be obtained from any other Set implementations. However, in the specific internal implementation, each EnumSet content is expressed as a bitvector. If the underlying Enumeration type has 64 or fewer elements, most of them do. The entire EnumSet is represented by a single long, so its performance is better than that of the upper-level domain. Batch processing, such as removeAll and retainAll, are implemented using bit algorithms. It is implemented just like manually replacing a bit domain. However, you can avoid errors and unsightly code during manual operations, because EnumSet has completed this arduous 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)); }}
Run test() , Output[BOLD, ITALIC] .
The EnumSet class combines the conciseness of bitwise domains, the performance advantages, and all the advantages of enumeration types. Therefore, we have no reason to use bitwise notation. Unless it is a communication protocol field.
But in fact, EnumSet also has a disadvantage-it cannot create immutable EnumSet, but we can use Collections. unmodifiableSet to encapsulate EnumSet, but its simplicity and performance will be 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 an element to unmodifiableSet, java. lang. UnsupportedOperationException will be reported.
Summary
The above is all the details about how to replace bitwise domain code with EnumSet in Java. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!