简体   繁体   English

在enumSet中存储不同的枚举类型

[英]store different enum types in enumSet

I want to store different enum types within one enum. 我想在一个枚举中存储不同的枚举类型。

Is this the right (shortest) way? 这是正确的(最短的)方法吗?

public enum MyEnums {
    all(EnumSet.of(Color.red, Shape.round));

    MyEnums(EnumSet<? extends Enum<?>> keys) {
        this.keys = keys;
    }

    private final Set<? extends Enum<?>> keys;

    public Set<? extends Enum<?>> getKeys() {
        return keys;
    }
}

Use a different kind of set. 使用另一种类型的集。 EnumSet is designed to hold enum values of a single kind only : EnumSet 设计为仅保留一种枚举值

All of the elements in an enum set must come from a single enum type 枚举集中的所有元素都必须来自单个枚举类型

The point is that it is very efficient because it stores a bit mask of the present ordinal values - most enums have fewer than 64 values, so all it basically needs in terms of member variables is the Class and a long . 关键是它非常有效,因为它存储了当前序数值的位掩码-大多数枚举数都少于64个值,因此就成员变量而言,它所需要的基本上是Classlong (There is a private subclass of EnumSet called something like JumboEnumSet which handles larger enums). (有一个EnumSet的私有子类,称为JumboEnumSet之类,可以处理较大的枚举)。

If you have multiple enum types, it can't distinguish values from different enums with the same ordinal. 如果您有多个枚举类型,则无法区分具有相同序数的不同枚举值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM