根据枚举对应值来转化的技巧

Posted flyduckforever

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了根据枚举对应值来转化的技巧相关的知识,希望对你有一定的参考价值。

根据枚举对应值来转化的技巧

常规

switch (x) {
    case "1":
        x = Globals.type.first.getvalue());
        break;
    case "2":
        x = Globals.type.second.getvalue());
        break;
    case "3":
        x = Globals.type.third.getvalue());
        break;
    default:

        break;
}
存在问题:如果有许多外面套层循环很多x需要改正,这样效率很低

解决方法:

for (Globals.type e : Globals.type.values()) {
    map.put(e.getCode(),e.getValue());
}
x=map.get(x);

以上是关于根据枚举对应值来转化的技巧的主要内容,如果未能解决你的问题,请参考以下文章