通过其内部字段获取枚举 [英] Get enum by its inner field

查看:149
本文介绍了通过其内部字段获取枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



现在我需要通过内部字段获取枚举。



写道:

 包测试; 

/ **
*测试枚举以测试枚举=)
* /
public enum TestEnum {
ONE(1),TWO(2) ,三(3);

private int number;

TestEnum(int number){
this.number = number;
}

public TestEnum findByKey(int i){
TestEnum [] testEnums = TestEnum.values(); (TestEnum testEnum:testEnums)
{
if(testEnum.number == i){
return testEnum;
}
}
返回null;
}
}

但是查看所有枚举不是很有效每次我需要找到合适的实例。



有没有其他的方式做同样的事情?

解决方案

您可以使用一个静态Map< Integer,TestEnum> static code code code code code code code code code code code code code $ b

请注意, findByKey 已经被 static 号码也是 final

  import java.util 。*; 

public enum TestEnum {
ONE(1),TWO(2),SIXTY_NINE(69);

private final int number;
TestEnum(int number){
this.number = number;
}

private static final Map< Integer,TestEnum>地图;
static {
map = new HashMap< Integer,TestEnum>(); (TestEnum v:TestEnum.values())
{
map.put(v.number,v);
}
}
public static TestEnum findByKey(int i){
return map.get(i);
}

public static void main(String [] args){
System.out.println(TestEnum.findByKey(69)); //打印SIXTY_NINE

System.out.println(
TestEnum.values()== TestEnum.values()
); //打印false
}
}

code> findByKey 成为 O(1)操作。



参考文献





相关问题








code> values()



第二个 println code> main 方法是rev ealing: values()每次调用返回一个新分配的数组!只需调用 values()一次并缓存数组,原始的 O(N)但是该解决方案平均仍然是 O(N)


Have enum with inner fields, kind of map.

Now I need to get enum by its inner field.

Wrote this:

package test;

/**
 * Test enum to test enum =)
 */
public enum TestEnum {
    ONE(1), TWO(2), THREE(3);

    private int number;

    TestEnum(int number) {
        this.number = number;
    }      

    public TestEnum findByKey(int i) {
        TestEnum[] testEnums = TestEnum.values();
        for (TestEnum testEnum : testEnums) {
            if (testEnum.number == i) {
                return testEnum;
            }
        }
        return null;
    }
}

But it's not very efficient to look up through all enums each time I need to find appropriate instance.

Is there any other way to do the same?

解决方案

You can use a static Map<Integer,TestEnum> with a static initializer that populates it with the TestEnum values keyed by their number fields.

Note that findByKey has been made static, and number has also been made final.

import java.util.*;

public enum TestEnum {
    ONE(1), TWO(2), SIXTY_NINE(69);

    private final int number;    
    TestEnum(int number) {
        this.number = number;
    }

    private static final Map<Integer,TestEnum> map;
    static {
        map = new HashMap<Integer,TestEnum>();
        for (TestEnum v : TestEnum.values()) {
            map.put(v.number, v);
        }
    }
    public static TestEnum findByKey(int i) {
        return map.get(i);
    }

    public static void main(String[] args) {
        System.out.println(TestEnum.findByKey(69)); // prints "SIXTY_NINE"

        System.out.println(
            TestEnum.values() == TestEnum.values()
        ); // prints "false"
    }
}

You can now expect findByKey to be a O(1) operation.

References

Related questions


Note on values()

The second println statement in the main method is revealing: values() returns a newly allocated array with every invokation! The original O(N) solution could do a little better by only calling values() once and caching the array, but that solution would still be O(N) on average.

这篇关于通过其内部字段获取枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆