Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中使用enum键值_Java_Enumeration - Fatal编程技术网

如何在java中使用enum键值

如何在java中使用enum键值,java,enumeration,Java,Enumeration,我想在Java11中创建一个带有键值的枚举类 我创建了这样一个枚举 public 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 =

我想在Java11中创建一个带有键值的枚举类 我创建了这样一个枚举

public 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;
    }
}
[
"ACTIVE",
"INACTIVE"
]
[
{
"Key": "Inactive", 
"value":"2"
},
{
"Key": "Active",
 "value":"1"
}
]
问题是当我执行Saison Saison.getvalues()时 我是这样的

public 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;
    }
}
[
"ACTIVE",
"INACTIVE"
]
[
{
"Key": "Inactive", 
"value":"2"
},
{
"Key": "Active",
 "value":"1"
}
]
但我想这样

public 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;
    }
}
[
"ACTIVE",
"INACTIVE"
]
[
{
"Key": "Inactive", 
"value":"2"
},
{
"Key": "Active",
 "value":"1"
}
]

如何调用我的enum tio以获得这样的结果

没有什么可以阻止您返回包含
键、值
对的映射条目

public 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;
    }
}
[
"ACTIVE",
"INACTIVE"
]
[
{
"Key": "Inactive", 
"value":"2"
},
{
"Key": "Active",
 "value":"1"
}
]
 enum Status {

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

    private final String key;
    private final int value;

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

    public String getKey() {
        return key;
    }
    public int getValue() {
        return value;
    }
    public Entry<String,Integer> getBoth() {
        return new AbstractMap.SimpleEntry<>(key, value);
    }   
}

Entry<String,Integer> e = Status.ACTIVE.getBoth();
System.out.println("Key: = " + e.getKey());
System.out.println("Value: = " + e.getValue());
印刷品

Key: = Active
Value: = 1
Active=1
"key": Active",
"value": "1"

    
您还可以覆盖枚举的toString并执行类似的操作

public String toString() {
    return String.format("\"key\": \"%s\",%n\"value\": \"%s\"",
            getKey(), getValue());
}

System.out.println(Status.ACTIVE);
印刷品

Key: = Active
Value: = 1
Active=1
"key": Active",
"value": "1"

    

你的意思是当试图编码为json时?`saison.getvalues()`好吧,在你的问题中没有
saison
。此外,类名应该是PascalCase,因此
Status
Saison
,而不是
Status
Saison
。另外,输出应该是JSON吗?如果是这样的话,你可能想寻找专门的工具来解决这个问题。你在做什么?Java枚举没有getValues()方法。。。什么是赛森?非常感谢你的帮助,现在已经开始工作了。