Java EnumSet中枚举的限制数是如何产生的?

Java EnumSet中枚举的限制数是如何产生的?,java,enums,enumset,Java,Enums,Enumset,我发现EnumSet中使用了有限数量的枚举(64)。请参阅下面EnumSet源代码中的方法(此代码从JDK1.7中捕获) /** *创建具有指定元素类型的空枚举集。 * *@param elementType此枚举的元素类型的类对象 *设置 *如果elementType为null,@将引发NullPointerException */ 公共静态枚举集noneOf(类elementType){ 枚举[]宇宙=getUniverse(elementType); if(universe==null)

我发现EnumSet中使用了有限数量的枚举(64)。请参阅下面EnumSet源代码中的方法(此代码从JDK1.7中捕获)

/**
*创建具有指定元素类型的空枚举集。
*
*@param elementType此枚举的元素类型的类对象
*设置
*如果elementType为null,@将引发NullPointerException
*/
公共静态枚举集noneOf(类elementType){
枚举[]宇宙=getUniverse(elementType);
if(universe==null)
抛出新的ClassCastException(elementType+“不是枚举”);

如果(universe.length不是,则仅限于枚举中可用的不同类型:


但不限于枚举中可用的不同类型:


它不限于64项。如果超过64项,则选择不同的实现。我还没有查看源代码,但我猜
RegularEnumSet
是使用单个
long

实现为位掩码的。它不限于64项。如果有,则选择不同的实现超过64项。我还没有查看源代码,但我猜
RegularEnumSet
是使用单个
long

1实现为位掩码的。可以看到,在RegularEnumSet中,它使用64位长而不是数组。2.JumboEnumSet使用数组时速度较慢/更大,但它可以处理任何大小的枚举an have.谢谢你的信息,Peter.1.你可以在RegularEnumSet中看到,它使用64位长度而不是数组。2.JumboEnumSet使用数组时速度较慢/更大,但它可以处理任何大小的枚举。谢谢你的信息,Peter。
/**
 * Creates an empty enum set with the specified element type.
 *
 * @param elementType the class object of the element type for this enum
 *     set
 * @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);
}