EnumSet replaces bit field code in Java detail

  • 2021-01-19 22:14:15
  • OfStack

This paper mainly studies the relevant content of EnumSet replacing bit fields in Java, which is introduced as follows.

Reading Notes Effective Java Chinese Version 2

The bit-field notation allows for efficient collection operations such as union (union) and intersection (intersection) using bit-field operations. But bit fields have all the drawbacks of ES11en enumeration, and more. When bitfield 1 is printed digitally, translating bitfields is much more difficult than translating simple int enumeration constants. There is even no easy way to traverse all the elements represented in a bit field.


//Bit field enumeration constant - OBSOLETE
public 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 enumerated type. This class implements the Set interface, providing rich functionality, type safety, and interoperability that you would expect from any other Set implementation. However, in the internal concrete implementation, each EnumSet content is represented as a bit vector. If the underlying enumeration type has 64 or fewer elements -- most do. The entire EnumSet is represented as a single long, so its performance is comparable to that of the upper domain. Batch processing, such as removeAll and retainAll, is implemented using bit algorithms. It is implemented as a manual replacement for bit-fields. But you can avoid manual errors and unsightly code because EnumSet does the hard work for you. `


//EnumSet - a modern replacement for bit fields
public 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));
  }
}

perform test() And the output [BOLD, ITALIC] .

The ES34en class combines the simplicity and performance advantages of the bit-field with all the advantages of enumerated types, so there is no reason to use bit-field notation. Unless it is a communication protocol field.

In fact, EnumSet has one drawback -- it cannot create immutable EnumSet, but we can encapsulate 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);
}

java.lang.UnsupportedOperationException = java.lang.UnsupportedOperationException = java.lang.UnsupportedOperationException = java.lang.UnsupportedOperationException

conclusion

The above is this article about Java in EnumSet instead of bit-field code detailed explanation of all the content, I hope to help you. Interested friends can continue to refer to the site of other related topics, if there are shortcomings, welcome to leave a message to point out. Thank you for your support!


Related articles: