从代码值中查找 Java Enum 类型?

发布于 2024-10-18 09:37:53 字数 650 浏览 1 评论 0原文

我们在数据库中使用代码值,在 Java 中使用枚举。查询数据库时,我们需要获取一个代码值并获取一个Enum实例。

使用 HashMap 来避免迭代是否太过分了?你会怎么办?有更简单的方法吗?

public enum SomeEnum
{
    TYPE_A(2000), TYPE_B(2001);

    private int codeValue;

    private static HashMap<Integer, SomeEnum> codeValueMap = new HashMap<Integer, SomeEnum>(2);

    static
    {
        for (SomeEnum  type : SomeEnum.values())
        {
            codeValueMap.put(type.codeValue, type);
        }
    }

    //constructor and getCodeValue left out      

    public static SomeEnum getInstanceFromCodeValue(int codeValue)
    {
        return codeValueMap.get(codeValue);
    }
}

We use code values in our database, and Enums in Java. When querying the database, we need to take a code value and get an Enum instance.

Is it overkill to have a HashMap to avoid iteration? What would you do? Is there an easier way?

public enum SomeEnum
{
    TYPE_A(2000), TYPE_B(2001);

    private int codeValue;

    private static HashMap<Integer, SomeEnum> codeValueMap = new HashMap<Integer, SomeEnum>(2);

    static
    {
        for (SomeEnum  type : SomeEnum.values())
        {
            codeValueMap.put(type.codeValue, type);
        }
    }

    //constructor and getCodeValue left out      

    public static SomeEnum getInstanceFromCodeValue(int codeValue)
    {
        return codeValueMap.get(codeValue);
    }
}

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

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

发布评论

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

评论(6

雅心素梦 2024-10-25 09:37:53

这正是我解决该特定问题所采取的方法。从设计的角度来看,我认为它没有任何问题,它直观、高效,并且(据我所知)完全做到了它应该做的事情。

我能想到的唯一其他明智的方法是将地图放在单独的类中,然后调用该类以从 SomeEnum 的构造函数更新地图。根据用例,这种分离可能是有益的 - 但除非它有硬性好处,否则我会采用您的方法并将所有内容封装在枚举本身中。

That's exactly the approach I'd take to solve that particular problem. I see nothing wrong with it from a design point of view, it's intuitive, efficient and (as far as I can see) does exactly what it should.

The only other sensible approach I can think of would be to have the map in a separate class and then call that class to update the map from SomeEnum's constructor. Depending on the use case, this separation could be beneficial - but unless it would have a hard benefit I would take your approach and encapsulate everything within the enum itself.

南街九尾狐 2024-10-25 09:37:53

谢谢,我想我主要关心的是内存使用情况,以及是否值得。

除非该枚举具有数千个值,否则内存使用量将微不足道。 (如果它确实有数千个值,那么使用迭代来进行查找将是一个主要的性能杀手。)在

我看来,这是对内存的合理使用。

也许我想多了。

也许你是。

Thanks, I guess my main concern is memory usage, and if it is worth it.

Unless that enum has thousands of values, memory usage will be trivial. (And if it does have thousands of values, then using iteration to do the lookup would be a major performance killer.)

This is a sensible use of memory, IMO.

Perhaps I am over thinking this.

Perhaps you are.

梦中的蝴蝶 2024-10-25 09:37:53

我认为在这种情况下我们无法避免迭代。要么是 HashMap 来做,要么我们自己写迭代代码。
如果性能确实很重要,也许您可​​以尝试二叉树方法。

I think in this case we can't avoid iteration. It's either HashMap doing it, or we wrote our own iteration code.
If performance really does matter maybe you can try a binary tree approach.

趴在窗边数星星i 2024-10-25 09:37:53

如果您的枚举空间很密集,即没有很多未使用的值,则可以使用 toString() 和 valueOf() 方法。使用公共字符串前缀命名您的值,然后在使用 valueOf() 之前附加前缀,并在使用 toString() 之后删除它。这样做的缺点是,如果数据库中存储的是数值,则必须将其转换为数值。

或者,您可以添加常用的转换方法并将数据库值分配给特定的枚举值。

这两种技术都具有利用枚举类设计的优点。

http:// mindprod.com/jgloss/enum.html

不过,如果你的方法能完成你想要的工作,那就没有问题。

If your enum space is dense, that is, not a lot of unused values, you could use the toString() and valueOf() methods. Name your values with a common string prefix, then attach the prefix before using valueOf() and strip it after using toString(). This has the disadvantage that you would have to convert to a numeric value if that's how it's stored in your database.

Alternatively, you could add common methods for conversion and assign your database value to a specific enum value.

Both these techniques have the advantage of leveraging the design of enum classes.

There is a lot of good, mind-bending information about enums (and Java, in general) at http://mindprod.com/jgloss/enum.html.

Though, there's nothing wrong with your way if it does the job you want.

枯叶蝶 2024-10-25 09:37:53

没关系。不必担心微小的性能差异。

人们可能会认为,如果枚举只有两个实例,就像您的示例一样,那么简单的迭代代码会更快:

public static SomeEnum getInstanceFromCodeValue(int codeValue)
{
    for (SomeEnum  type : SomeEnum.values()) {
        if(type.codeValue == codeValue)
            return type;
    }
}

但是,如果我们确实关心这种级别的性能,则存在隐藏的成本,而且成本相当昂贵。它是可以修复的,但你需要先看看它:)

That's fine. Don't worry about tiny performance differences.

One would think that if there are only two instances for an enum, like in your example, a trivial code of iterating would be faster:

public static SomeEnum getInstanceFromCodeValue(int codeValue)
{
    for (SomeEnum  type : SomeEnum.values()) {
        if(type.codeValue == codeValue)
            return type;
    }
}

But there's a hidden cost, quite expensive one if we do care about performane at such level. It's fixable, but you need to see it first:)

烟凡古楼 2024-10-25 09:37:53

获取 ID:

EnumDay day = EnumDay.WEDNESDAY;
int myID = day.ordinal();

从 myID 加载日期:

EnumDay dayCopy = EnumDay.values()[myID];

To get the ID:

EnumDay day = EnumDay.WEDNESDAY;
int myID = day.ordinal();

To load the day from the myID:

EnumDay dayCopy = EnumDay.values()[myID];

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