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. [Python]同是新手的我,分享一些经验

    本来想分享一些知识,但是发现,写着写着,感觉分享一些经验,或许会少让大家走一些弯路,索性就把标题一改. 我不是教给大家些什么,我没有资格,我也是摸着石头过河,我只是本着分享的精神,在这里分享自己的东西 ...

  2. 一些有用的UtilityExtend小方法

    public static bool StartBy(this string thisValue, params string[] startBy) { foreach (string item in ...

  3. mysql根据身份证信息来获取用户属性信息

    需要:根据身份证信息来获取用户属性 方法:可以使用如下sql语句: ) ' then '北京市' ' then '天津市' ' then '河北省' ' then '山西省' ' then '内蒙古自 ...

  4. 点餐系统mealsystem.sql

    /* Navicat MySQL Data Transfer Source Server : localhost Source Server Version : 50162 Source Host : ...

  5. Equeue初识

    详细解说: http://www.cnblogs.com/netfocus/p/3595410.html 简单代码用法: Producer 端代码用法实例 和 Customer 端代码用法示例: ht ...

  6. PHP实现文字水印图片

    php实现简单的文字水印图片,使用前需要开启php配置中的gd2功能 <?php/*打开图片*/ //1.配置图片路径 $src="image/55.jpg";//这个路径改 ...

  7. 百度FIS入门

    1.fis作为nodejs的模块来管理的,所以首先得安装nodejs,看我前面的安装nodejs的文章. 2.官方的案例下载包https://github.com/hefangshi/fis-quic ...

  8. java事务理解

    还在学Hibernate,后续一大堆概念刚接触需要理解.觉得-——事务——这个概念不是很好理解,所以发上来记录一下. 首先说点千篇一律的东西.概念和特性都是随处可见的,无论哪里都很容易找到,关键是你如 ...

  9. 个人收集整理的5Ucms标签

    {field:cid} 当前栏目id {field:id}  当前页面id {field:content} 当前页面内容 [List:Modifytime $format=yy-mm-dd] 文章发布 ...

  10. LIST-PROCESSING用法 ABAP任意时刻进行List输出_SAP

    如何在SAP的Screen中编写List报表 1.相关命令LEAVE TO LIST-PROCESSING [AND RETURN TO SCREEN <nnnn>].LEAVE LIST ...