java - 如何匹配java枚举

标签 java enums

我有一个这样的枚举:

public enum ChartType
{   
    TABLE(0, false), BAR(1, false), COLUMN(3, false)
    private int type;
    private boolean stacked;
    ChartType(int type, boolean stacked)
    {
        this.type = type;
        this.stacked = stacked;
    }
    public int getType()
    {
        return type;
    }
    public boolean isStacked()
    {
        return this.stacked;
    }
}

我从请求中得到一个图表类型(int 值,如 0、1、3)并想要匹配的输入

最佳答案

类似的东西。不确定语法是否是 100%,但它演示了这个想法。

public ChartType getChartypeForValue(int value)
    for(ChartType type : ChartType.values()){
        if(type.getType()  == value){
            return type;
        }
    }
    return null;
}

关于java - 如何匹配java枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28904113/

相关文章:

java - 从图库中选择图像并在 ImageView 中显示

java - 如何在 Websphere 上运行普通 Java 应用程序

typescript - 在 typescript 中将枚举类型作为参数传递

swift - 如何访问 Swift 中其他类中的枚举值?

MySQL Enum 总是包含 '' (空字符串)的可能性

python - 如何从模板中访问 Django 模型的 Enum?

java - 如何在Android中解析JSON数组(不是Json对象)

java - 刷新 RecyclerView 中的数据并保持其滚动位置将用户带到 Activity 的顶部

java - 当我在 Android Studio 的菜单中说添加地点时,它有时会打开 map ,有时应用程序会在不打开的情况下关闭

java - 在fasterxml中,反序列化json后,如果enum是类中的第一个属性,其他字段为null