如何在java中制作像枚举一样的键值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40547499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:47:37  来源:igfitidea点击:

How to make key value like enum in java

javajsonhibernateenumsalpacajs

提问by Saqib Ahmed

I need to make an Enumcontaining some strings with spaces and their values in intlike:

我需要制作一个Enum包含一些带有空格和它们的值的字符串,int例如:

public enum status{
Active(1),
Inactive(2);
}

because I am using it with hibernate and also will convert it to JSON for alpaca js forms.

因为我将它与 hibernate 一起使用,并且还将其转换为 JSON 以用于 alpaca js 表单。

like:

喜欢:

[{"text": "Inactive", "value":"2"},{"text": "Active", "value":"1"}]

I'm stuck in making enum. how to make such type of enum?

我被困在制作enum. 如何制作这种类型的enum

采纳答案by Chandana Kumara

You can not put space between strings. Instead of the you can use underscore as follows:

您不能在字符串之间放置空格。您可以使用下划线代替 ,如下所示:

In_Active

You can use this way:

你可以这样使用:

enum Status {

    ACTIVE("Active", 1), IN_ACTIVE("In Active", 2);

    private final String key;
    private final Integer value;

    Status(String key, Integer value) {
        this.key = key;
        this.value = value;
    }

    public String getKey() {
        return key;
    }
    public Integer getValue() {
        return value;
    }
}

回答by Someone

You can't put a space in the middle of an identifier.

您不能在标识符中间放置空格。

Check out this link Is it possible to assign numeric value to an enum in Java?for assigning the value to an enum in java.

查看此链接是否可以在 Java 中为枚举分配数值?用于将值分配给 java 中的枚举。

回答by GameDroids

You can hold multiple values in one enumand even have getters to handle them. Here is an example I used once (I try to adapt it to your problem):

您可以在一个中保存多个值enum,甚至可以使用 getter 来处理它们。这是我曾经使用过的一个示例(我尝试使其适应您的问题):

public enum Status{

    ACTIVE(1, "Active"),
    INACTIVE(2, "In Active");

    private final Integer value;
    private final String text;

    /**
     * A mapping between the integer code and its corresponding text to facilitate lookup by code.
     */
    private static Map<Integer, Status> valueToTextMapping;

    private Status(Integer value, String text){
        this.value = value;
        this.text = text;
    }

    public static Status getStatus(Integer i){
        if(valueToTextMapping == null){
            initMapping();
        }
        return valueToTextMapping.get(i);
    }

    private static void initMapping(){
        valueToTextMapping = new HashMap<>();
        for(Status s : values()){
            valueToTextMapping.put(s.value, s);
        }
    }

    public Integer getValue(){
        return value;
    }

    public String getText(){
        return text;
    }

    @Override
    public String toString(){
        final StringBuilder sb = new StringBuilder();
        sb.append("Status");
        sb.append("{value=").append(value);
        sb.append(", text='").append(text).append('\'')
        sb.append('}');
        return sb.toString();
    }
}

So in your code you can simply use Status.ACTIVEand it will represent an instance of your Enum, that holds valueand textthe way you want it

因此,在你的代码,你可以简单地使用Status.ACTIVE,这将代表您的枚举的实例,保存valuetext你想要的方式