Data structure - EnumSet class

Brief introduction

Objects with an enumeration class if HashSet to put on too much wasted space, because the internal HashSet or HashMap using the hash way for an enumeration class have been identified before running for the order, and they will not be particularly enumerate many continue to use HashSet more harm than good, JDK provided EnumSet is devoted to the service of enumerated types, and other Set EnumSet not the same place, it is ordered and not allowed to insert null

EnumSet class
public abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E>
        implements Cloneable, java.io.Serializable

EnumSet value must be enumerated values, and EnumSet is an abstract class, the implementation class is EnumMap

EnumSet property
// 保存的数据类型
final Class<E> elementType;
// 保存数据的数组
final Enum<?>[] universe;
// 空枚举
private static Enum<?>[] ZERO_LENGTH_ENUM_ARRAY = new Enum<?>[0];
EnumSet Constructor
EnumSet(Class<E>elementType, Enum<?>[] universe) {
    this.elementType = elementType;
    this.universe    = universe;
}

, Only to be used subclass inherits the constructor is not public

EnumSet based approach
// 生成一个空的EnumSet,并指定其数据类型
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);
}

EnumSet has two subclasses: RegularEnumSet, JumboEnumSet, if the number of enumeration element is greater than 64, then you are really creating JumboEnumSet implementation class, otherwise it is RegularEnumSet implementation class

// 枚举类转EnumSet
public static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType) {
    EnumSet<E> result = noneOf(elementType);
    result.addAll();
    return result;
}
// 初始集合包括枚举值中指定范围的元素
public static <E extends Enum<E>> EnumSet<E> range(E from, E to) {
    if (from.compareTo(to) > 0)
        throw new IllegalArgumentException(from + " > " + to);
    EnumSet<E> result = noneOf(from.getDeclaringClass());
    result.addRange(from, to);
    return result;
}
Method EnumSet of
public static <E extends Enum<E>> EnumSet<E> of(E e) {
    EnumSet<E> result = noneOf(e.getDeclaringClass());
    result.add(e);
    return result;
}
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2) {
    EnumSet<E> result = noneOf(e1.getDeclaringClass());
    result.add(e1);
    result.add(e2);
    return result;
}
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3) {
    EnumSet<E> result = noneOf(e1.getDeclaringClass());
    result.add(e1);
    result.add(e2);
    result.add(e3);
    return result;
}
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4) {
    EnumSet<E> result = noneOf(e1.getDeclaringClass());
    result.add(e1);
    result.add(e2);
    result.add(e3);
    result.add(e4);
    return result;
}
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4,
                                                E e5)
{
    EnumSet<E> result = noneOf(e1.getDeclaringClass());
    result.add(e1);
    result.add(e2);
    result.add(e3);
    result.add(e4);
    result.add(e5);
    return result;
}
public static <E extends Enum<E>> EnumSet<E> of(E first, E... rest) {
    EnumSet<E> result = noneOf(first.getDeclaringClass());
    result.add(first);
    for (E e : rest)
        result.add(e);
    return result;
}

EnumSet There are many forms of overloaded methods, the last variable parameter is accepted, other overloaded methods appears to be superfluous, the reason why there are other overloaded methods because of the low operating efficiency variable number of parameters.

Guess you like

Origin www.cnblogs.com/yuanjiangnan/p/12642391.html