如何在Java中使用枚举键值

huangapple go评论66阅读模式
英文:

How to use enum key value in java

问题

我想在Java 11中创建一个枚举类,具有键值对。

我创建了一个枚举类,如下所示:

public enum Status {
    ACTIVE("Active", 1), INACTIVE("Inactive", 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;
    }
}

问题在于,当我执行 Status.values() 时,我得到了以下结果:

[
"ACTIVE",
"INACTIVE"
]

但我想要得到如下结果:

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

如何调用我的枚举类以获得这样的结果呢?

英文:

I want to create an enum class in java 11 with key value
I create a enum like this

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;
    }
}

the problem that when i do Saison saison.getvalues()
i got like this

[
"ACTIVE",
"INACTIVE"
]

But i want to got like this

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

how can i call my enum tio get a result like this

答案1

得分: 3

没有任何东西可以阻止你返回一个包含key,value键值对的映射项。

 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());

或者打印Entry的toString()值。

System.out.println(e);

输出

Key: = Active
Value: = 1
Active=1

你还可以重写Enum的toString方法,像这样做。

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

System.out.println(Status.ACTIVE);

输出

"key": "Active",
"value": "1"
英文:

There is nothing to prevent you from returning a map entry which contains the key,value pair.

 enum Status {

    ACTIVE(&quot;Active&quot;, 1), IN_ACTIVE(&quot;In Active&quot;, 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&lt;String,Integer&gt; getBoth() {
    	return new AbstractMap.SimpleEntry&lt;&gt;(key, value);
    }	
}

Entry&lt;String,Integer&gt; e = Status.ACTIVE.getBoth();
System.out.println(&quot;Key: = &quot; + e.getKey());
System.out.println(&quot;Value: = &quot; + e.getValue());

or print the toString() value of the Entry.

System.out.println(e);
	

Prints

Key: = Active
Value: = 1
Active=1

You can also override toString of your Enum and do something like this.

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

System.out.println(Status.ACTIVE);

Prints

&quot;key&quot;: Active&quot;,
&quot;value&quot;: &quot;1&quot;

	

</details>



# 答案2
**得分**: 0

```java
public enum FileCode {
    AB("AB", "AB 描述"),
    CD("CD", "CD 描述"),
    EF("EF", "EF 描述");

    private final String key;
    private final String value;
    private final KeyValue keyValue;

    FileCode(String key, String value) {
        this.key = key;
        this.value = value;
        this.keyValue = new KeyValue(key, value);
    }

    public static List<KeyValue> getAllKeyValue() {
        List<KeyValue> keyValueList = new ArrayList<>();
        for (FileCode f : FileCode.values()) {
            keyValueList.add(KeyValue.builder()
                    .key(f.key)
                    .value(f.value)
                    .build());
        }
        return keyValueList;
    }

    public Map.Entry<String, String> getBoth() {
        return new AbstractMap.SimpleEntry<>(key, value);
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class KeyValue {
        String key;
        String value;
    }
}
英文:

I did in this way to return all key and value in list.

public enum FileCode {
    AB(&quot;AB&quot;, &quot;AB description&quot;),
    CD(&quot;CD&quot;, &quot;CD description&quot;),
    EF(&quot;EF&quot;, &quot;EF description&quot;);

    private final String key;
    private final String value;
    private final KeyValue keyValue;

    FileCode(String key, String value) {
        this.key = key;
        this.value = value;
        this.keyValue = new KeyValue(key, value);
    }

    public static List&lt;KeyValue&gt; getAllKeyValue() {
        List&lt;KeyValue&gt; keyValueList = new ArrayList&lt;&gt;();
        for (FileCode f : FileCode.values()) {
            keyValueList.add(KeyValue.builder()
                    .key(f.key)
                    .value(f.value)
                    .build());
        }
        return keyValueList;

    }

    public Map.Entry&lt;String, String&gt; getBoth() {
        return new AbstractMap.SimpleEntry&lt;&gt;(key, value);
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class KeyValue {
        String key;
        String value;

    }

}

huangapple
  • 本文由 发表于 2020年9月28日 21:57:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64103674.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定