Bit fields is used for passing around sets of constants. Such as

// Bit field enumeration constants - OBSOLETE!

public class Text {

public static final int STYLE_BOLD = 1 << 0; // 1

public static final int STYLE_ITALIC = 1 << 1; // 2

public static final int STYLE_UNDERLINE = 1 << 2; // 4

public static final int STYLE_STRIKETHROUGH = 1 << 3; // 8

// Parameter is bitwise OR of zero or more STYLE_ constants

public void applyStyles(int styles) { ... }

}

text.applyStyles(STYLE_BOLD | STYLE_ITALIC);

Disadvantage of Bit field

  1. Harder to interpret a bit field than a simple int enum constant when it's printed as a number.
  2. Hard to iterate over all of the elements represented by a bit field.

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) { ... }

}

text.applyStyles(EnumSet.of(Style.BOLD, Style.ITALIC) );

Advantage of EnumSet

  1. Richness, type safety and interoperability of Set implementation.
  2. Bulk operations, such as removeAll and retainAll, are implemented using bit-wise arithmetic, just as you'd do manually for bit fields.

Disadvantage of EnumSet

It's not immutable before release 1.6.

Note

If the underlying enum type has sixty-four or fewer elements—and most do—the entire EnumSet is represented with a single long, so its performance is comparable to that of a bit field.

Summary

Because an enumerated type will be used in sets, there is no reason to represent it with bit fields. The EnumSet class combines the conciseness and performance of bit fields with all the many advantages of enum types described in Item 30. You can wrap an EnumSet with Collections.unmodifiable Set, but conciseness and performance will suffer.

Effective Java 32 Use EnumSet instead of bit fields的更多相关文章

  1. effective java——32用EnumSet代替位域

    什么是位域?为什么用到它?先来看一个例子: public class Test { public static final byte STYLE_BOLD = 1<<0; // 1 pub ...

  2. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  3. 《Effective Java》读书笔记 - 6.枚举和注解

    Chapter 6 Enums and Annotations Item 30: Use enums instead of int constants Enum类型无非也是个普通的class,所以你可 ...

  4. Effective Java 第三版——32.合理地结合泛型和可变参数

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  5. Effective Java 第三版——36. 使用EnumSet替代位属性

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  6. Effective Java通俗理解(下)

    Effective Java通俗理解(上) 第31条:用实例域代替序数 枚举类型有一个ordinal方法,它范围该常量的序数从0开始,不建议使用这个方法,因为这不能很好地对枚举进行维护,正确应该是利用 ...

  7. 《Effective Java(中文第二版)》【PDF】下载

    <Effective Java(中文第二版)>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382186 Java(中文第二版)& ...

  8. Java 高效编程(Effective Java)中文第三版(补档)

    来源:sjsdfg/effective-java-3rd-chinese <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过, ...

  9. Effective java笔记(二),所有对象的通用方法

    Object类的所有非final方法(equals.hashCode.toString.clone.finalize)都要遵守通用约定(general contract),否则其它依赖于这些约定的类( ...

随机推荐

  1. js-异常处理语句

    程序运行过程中难免会出错,出错后的运行结果往往是不正确的,因此运行时出错的程序通常被强制中止.运行时的错误统称为异常,为了能在错误发生时得到一个处理的机会,JavaScript提供了异常处理语句.包含 ...

  2. FXForms,自动生成iOS表单

    1.简介 FXForms是一个简单的表单提交框架,他的作者是鼎鼎大名的 Nick Lockwood,你也许听说过他的其他的一些框架,比如 iCarousel. 为什么使用FxForms? 表单处理简单 ...

  3. sql 去重

    ;WITH CETAS (SELECT *, ROW_NUMBER() OVER (PARTITION BY SearchTask_PKID ORDER BY SearchTask_PKID) Row ...

  4. windows下react-native环境搭建

    首先不得不先吐槽一下自己,一个坑总是踩很多次,且乐此不疲. 咋办? 写博客记录记录呗. 零.记录的点 Java环境的下载与配置 Android环境的下载与配置 Node环境的下载与配置 创建第一个re ...

  5. 修改memcached服务的端口号

    windows下修改memcached服务的端口号(默认端口:11211) 如果不是作为服务启动memcached的话,memcached -p 端口号就可以了. 通过修改注册表可以简单实现 运行:r ...

  6. markdown这么好用的东西我才知道。。。多么不折腾的我。。。

    markdown 锚点 努力吧 我的网站 之前有个域名phifan.com没续费被抢了,之后又买了phifan.cn没续费被抢了,还剩下个plusnet.cn说什么也不能再丢掉了! package c ...

  7. 泛函编程(10)-异常处理-Either

    上节我们介绍了新的数据类型Option:一个专门对付异常情况出现时可以有一致反应所使用的数据类型.Option可以使编程人员不必理会出现异常后应该如何处理结果,他只是获得了一个None值,但这个Non ...

  8. Ajax基础实例

    前端代码 <script type="text/javascript"> var xmlhttp; function go(url) { xmlhttp=null; i ...

  9. .NET Core的“dotnet restore”、“dotnet build”和“dotnet run”命令都是用来干什么的?

    dotnet restore 源代码:https://github.com/dotnet/cli/tree/rel/1.0.0/src/dotnet/commands/dotnet-restore 入 ...

  10. web api post注意事项

    黄色笑话 近期一个项目需要使用到web api 在使用过程中,对应get请求方式的使用基本没出什么问题 但是对于post请求状况百出. 今将遇到的的问题列出.以作借鉴. 问题  -----post方式 ...