文章作者:Tyan
部落格:noahsnail.com 1. EnumSet
EnumSet是Java Set介面的一個特別實現,在JDK 1.5中開始支援,Enum類型也正式引入到了Java中。與其它儲存枚舉常量的Set相比,EnumSet具有更好的效能,同時其也是Java中的優秀特性之一。下面從三個方面來介紹EnumSet,what,how,when。 2. What is EnumSet
EnumSet是Set介面的一個實現,它只能用來儲存Enum常量或其子類,不能儲存其它類型。EnumSet是設計模式中Factory 方法建立執行個體的一個很好例子。
EnumSet被聲明為abstract class類型,EnumSet有兩種實現方式,RegularEnumSet和JumboEnumSet,但是這兩種實現方式是包私人的,不能在包外訪問,因此必須使用Factory 方法來建立並返回EnumSet執行個體,不能通過建構函式來建立。EnumSet中提供了多種建立EnumSet執行個體的靜態Factory 方法,例如of方法(進行了函數重載),copyOf方法,noneOf方法等。 3. How EnumSet is implemented in Java
上面已經說了,EnumSet是一個抽象類別,有兩個具體實現:java.util.RegularEnumSet和java.util.JumboEnumSet。二者的主要區別在於前者使用long來表示元素的數量,而後者使用long[]來表示元素的數量。這二者中表示元素數量使用的是位域結構,即通過long的二進位位元來表示元素數量,例如:RegularEnumSet使用的是long表示元素數量,long數值是通過64位二進位表示的,因此其只能包含的元素最大數量為64,如果元素數目大於64,採用的是JumboEnumSet表示,JumboEnumSet的long[]中有一個long數值,就能表示64個元素,兩個long數值就能表示128個元素,以此類推。 4. When to use EnumSet in Java
《Effective Java》中的Item 32講述了一個EnumSet的使用情境,推薦去看一下。當你需要對枚舉類型進行特定的分組時,你可以使用EnumSet。例如,一周中有七天,你想將周末單獨分出來的時候(enum int pattern):
private enum DayOfWeek{ MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;}private EnumSet weekend = EnumSet.of(SATURDAY,SUNDAY);5. Important points about EnumSet
一個EnumSet中只能包含一種枚舉類型。
EnumSet中不能放入null元素,放入會拋出null 指標異常。
EnumSet是線程非安全的。
EnumSet的Iterator是自動防故障和弱一致的,不會拋出並發修改異常,即在迭代過程的中的修改結果不一定會在迭代過程中顯示。
EnumSet是高效能的Java集合。由於是基於數組的訪問,因此add,contains,next方法的時間複雜度為O(1)。
儲存枚舉常量時使用EnumSet而不要用HashSet。 6. Source Code of EnumSet
EnumSet:
public abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E> implements Cloneable, java.io.Serializable { ... EnumSet(Class<E>elementType, Enum<?>[] universe) { this.elementType = elementType; this.universe = universe; } /** * Creates an empty enum set with the specified element type. * * @param <E> The class of the elements in the set * @param elementType the class object of the element type for this enum * set * @return An empty enum set of the specified type. * @throws NullPointerException if <tt>elementType</tt> is null */ public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType) { Enum<?>[] universe = getUniverse(elementType); if (universe == null) throw new ClassCastException(elementType + " not an enum"); if (universe.length <= 64) return new RegularEnumSet<>(elementType, universe); else return new JumboEnumSet<>(elementType, universe); } ...}
RegularEnumSet:
class RegularEnumSet<E extends Enum<E>> extends EnumSet<E> { ... /** * Bit vector representation of this set. The 2^k bit indicates the * presence of universe[k] in this set. */ private long elements = 0L; RegularEnumSet(Class<E>elementType, Enum<?>[] universe) { super(elementType, universe); } ...}
JumboEnumSet:
class JumboEnumSet<E extends Enum<E>> extends EnumSet<E> { private static final long serialVersionUID = 334349849919042784L; /** * Bit vector representation of this set. The ith bit of the jth * element of this array represents the presence of universe[64*j +i] * in this set. */ private long elements[]; // Redundant - maintained for performance private int size = 0; JumboEnumSet(Class<E>elementType, Enum<?>[] universe) { super(elementType, universe); elements = new long[(universe.length + 63) >>> 6]; } ...}7. Example of EnumSet
import java.util.EnumSet;import java.util.Set;public class EnumSetDemo { private enum Color { RED(255, 0, 0), GREEN(0, 255, 0), BLUE(0, 0, 255); private int r; private int g; private int b; private Color(int r, int g, int b) { this.r = r; this.g = g; this.b = b; } public int getR() { return r; } public int getG() { return g; } public int getB() { return b; } } public static void main(String args[]) { // this will draw line in yellow color EnumSet<Color> yellow = EnumSet.of(Color.RED, Color.GREEN); drawLine(yellow); // RED + GREEN + BLUE = WHITE EnumSet<Color> white = EnumSet.of(Color.RED, Color.GREEN, Color.BLUE); drawLine(white); // RED + BLUE = PINK EnumSet<Color> pink = EnumSet.of(Color.RED, Color.BLUE); drawLine(pink); } public static void drawLine(Set<Color> colors) { System.out.println("Requested Colors to draw lines : " + colors); for (Color c : colors) { System.out.println("drawing line in color : " + c); } }}Output:Requested Colors to draw lines : [RED, GREEN]drawing line in color : REDdrawing line in color : GREENRequested Colors to draw lines : [RED, GREEN, BLUE]drawing line in color : REDdrawing line in color : GREENdrawing line in color : BLUERequested Colors to draw lines : [RED, BLUE]drawing line in color : REDdrawing line in color : BLUE
參考資料:
1、https://jaxenter.com/enumset-in-java-regularenumset-vs-jumboenumset-106051.html
2、Effective Java 2.0版本中的Item 1,本文就是在看《Effective Java》時看到的EnumSet,才想要仔細研究一下EnumSet的。Item 32講述了EnumSet的使用情境。
3、http://brokendreams.iteye.com/blog/2267485
4、http://javarevisited.blogspot.kr/2014/03/how-to-use-enumset-in-java-with-example.html