枚举值().length 与私有字段

发布于 2024-08-11 10:39:09 字数 666 浏览 3 评论 0原文

我有这样的枚举:

public enum Configuration {
    XML(1),
    XSLT(10),
    TXT(100),
    HTML(2),
    DB(20);

    private final int id;
    private Configuration(int id) {
        this.id = id;
    }
    public int getId() { return id; }
}

有时我需要检查枚举中有多少个字段。最好的解决方案是什么? 我应该使用方法“values().length”吗?或者也许,我必须在枚举中创建常量字段,如下所示:

public enum Configuration {
    XML(1),
    XSLT(10),
    TXT(100),
    HTML(2),
    DB(20);

    private final int id;
    private Configuration(int id) {
        this.id = id;
    }
    public int getId() { return id; }

    public static final int Size = 5;
}

什么是最快且更优雅的解决方案?

I have enumeration like this:

public enum Configuration {
    XML(1),
    XSLT(10),
    TXT(100),
    HTML(2),
    DB(20);

    private final int id;
    private Configuration(int id) {
        this.id = id;
    }
    public int getId() { return id; }
}

Sometimes I need to check how many fields I have in enumeration. What is the best solution?
Should I use a method "values().length"? Or maybe, I must create constant field in enumeration like this:

public enum Configuration {
    XML(1),
    XSLT(10),
    TXT(100),
    HTML(2),
    DB(20);

    private final int id;
    private Configuration(int id) {
        this.id = id;
    }
    public int getId() { return id; }

    public static final int Size = 5;
}

What is the fastest and more elegant solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜深人未静 2024-08-18 10:39:09

每次调用时,使用 values().length 都会创建数组的新副本。我有时会创建自己的 List (或设置或映射,无论我需要什么)以避免这种毫无意义的复制。不过,我不会对其进行硬编码...如果您只需要大小,我只需

private static final int size = Configuration.values().length;

在末尾使用:。在评估时,所有值都将被初始化。这避免了其他答案中提出的 DRY 和不一致问题。

当然,这本身就是一种微观优化……但最终会得到更简单的代码,IMO。从其他地方调用 values().length 并不能表达您感兴趣的内容,这只是枚举的大小 - 事实上您是通过在我看来,一系列的价值观是偶然的和分散注意力的。

使用 values() 的替代方法是使用 EnumSet.allOf().size() ,对于小型枚举来说,它会非常便宜 - 但同样,它的可读性不如只是有一个 size 字段。

Using values().length will create a new copy of the array every time you call it. I sometimes create my own List (or set, or map, whatever I need) to avoid this pointless copying. I wouldn't hard-code it though... if you only need the size, I'd just use:

private static final int size = Configuration.values().length;

at the end. By the time that is evaluated, all the values will have been initialized. This avoids the DRY and inconsistency concerns raised in other answers.

Of course, this is a bit of a micro-optimisation in itself... but one which ends up with simpler code in the end, IMO. Calling values().length from elsewhere doesn't express what you're interested in, which is just the size of the enum - the fact that you get at it through an array of values is incidental and distracting, IMO.

An alternative to using values() is to use EnumSet.allOf().size() which for small enums will be pretty cheap - but again, it's not as readable as just having a size field.

尽揽少女心 2024-08-18 10:39:09

我建议使用 values().length。这要优雅得多,并且与使用常量相比,性能开销可以忽略不计。此外,还消除了常量与枚举的实际长度不一致的风险。

I would recommend using values().length. This is far more elegant and the performance overhead versus using a constant will be negligable. Also, you eliminate the risk of the constant ever becoming out of step with the actual length of the enumeration.

月朦胧 2024-08-18 10:39:09

通过存储计数,您违反了DRY 原则,所以除非您有一个非常有充分的理由,你不应该。

By storing the count you're violating the DRY principle, so unless you have a very good reason, you shouldn't.

疾风者 2024-08-18 10:39:09

另一种方法是使用在 value() 方法之上初始化的常量。

public enum Colors {
    BLUE, GREEN, FUCHSIA;
    public static int length = Colors.values().length;
}

这样你就有了一个自动更新的常量,并且仍然避免了“values()”开销。

Another approach is to use a constant initialized on top of the values() method.

public enum Colors {
    BLUE, GREEN, FUCHSIA;
    public static int length = Colors.values().length;
}

This way you have an automatically updated constant and still avoid that "values()" overhead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文