1. Definitions:
Add special collections enum class elements
2. The difference with other collections:
The basic operation of the internal EnumSet implementation does not use a common data structures, such as arrays (the ArrayList), the list (the LinkedList), Ha-based table (HashMap, Hashtable, HashSet), red-black tree (TreeMap, TreeSet) but the completion of the operation using the bit set of
EnumSet is an abstract class, the method can only be constructed by a static factory EnumSet objects, as follows:
EnumSet <E> noneOf (Class < E> elementType): Constructs an empty set
EnumSet <E> allOf (Class < E> elementType): enumeration class structure comprising a set of all the enumerated items
EnumSet <E> of ( E e): structure comprising a set of elements
EnumSet <E> of (E e1 , E e2): structure 2 elements set
EnumSet <E> of (E e1 , E e2, E e3): structure comprising 3 a set of elements
EnumSet <E> of (E e1 , E e2, E e3, E e4): structure comprising four elements set
EnumSet <E> of (E e1 , E e2, E e3, E e4, E e5 ): structure comprises a set of five elements
EnumSet <E> of (E first , E ... rest): structure comprising a set of a plurality of elements (using variable parameter)
EnumSet <E> copyOf (EnumSet <E> S) : configuration parameters comprises the set of all elements in
EnumSet <E> copyOf (collection < E> c): a collection of all the elements comprising the structure parameter
3.EnumSet as a basic set of operations that the class method implementation principle (bit operation):
Description:
- From the EnumSet
noneOf
can be seen, when the enumeration class enumeration少于64
, the return isRegularEnumSet类
( )EnumSet的实现类
object, is greater than 64, returns the class object JumboEnumSet, for analytical purposes, later to explain the principles of the same class use RegularEnumSet
// EnumSet#noneOf 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); }
- The enumeration class test
enum Color{ RED("RED"), BLUE("BLUE"), YELLOW("YELLOW"), BLACK("BLACK"); String name; Color(String name){ this.name = name; } @Override public String toString(){ return this.name; } }
3.1 add method
public boolean add(E e) { TYPECHECK (e); long oldElements = elements; elements |= (1L << ((Enum<?>)e).ordinal()); return elements != oldElements; }
ORDINAL () is a sequence number for each of enumerated items, starting from 0, by declaration order, such as [RED, BLUE, YELLOW, BLACK ] corresponding to [0,1,2,3]; 1L << (( Enum <? >) e) .ordinal () (for convenience, referred to herein as enumerated value) indicates 1 * 2 ^ (e.ordinal () ), Color.RED the ordianl () is 0, the value 1 corresponding to enumerate * 2 ^ 0 = 1, in fact, is the first ordinal () + 1 bits (from right to left the number of bits is 1 bit) is 1 other bits of the decimal number 0, corresponding to the decimal number 00000001 (herein assumed to be 8bit, the actual type is long 32bit);
then each enumeration as follows:
Enumeration number 1L << ((Enum <?>) E) .ordinal () enumeration value
Enumeration | No. | 1L << ((Enum<?>)e).ordinal() | Enumeration values |
Color.RED | 0 | 00000001 | 1 |
Color.BLUE | 1 | 00000010 | 2 |
Color.YELLOW | 2 | 00000100 | 4 |
Color.BLACK | 3 | 00001000 | 8 |
elements |=
It is to enumerate the different elements of the value added in summing 相同
element 相或
when保持不变
3.2 remove method
public boolean remove(Object e) { if (e == null) return false; Class<?> eClass = e.getClass(); if (eClass != elementType && eClass.getSuperclass() != elementType) return false; long oldElements = elements; elements &= ~(1L << ((Enum<?>)e).ordinal()); return elements != oldElements; }
According to previous 枚举值相加
thinking, it is from Remove 总枚举值
subtracted 待删除元素的枚举值
, because 位运算
no direct subtraction, bit manipulation elements &= ~(1L << ((Enum<?>)e).ordinal());
is completed subtraction operation
3.3 contains method
public boolean contains(Object e) { if (e == null) return false; Class<?> eClass = e.getClass(); if (eClass != elementType && eClass.getSuperclass() != elementType) return false; return (elements & (1L << ((Enum<?>)e).ordinal())) != 0; }
contains methods to better understand the value of the enumeration values for each enumeration of all 不一样
, and 相互之间
carry out 相与
operations to 0
use 总枚举值
and 要查询的枚举项的枚举值
carry out 相与
the operation, if it is 0
explained 不存在
that the enumeration, otherwise存在