英文:
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("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());
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("\"key\": \"%s\",%n\"value\": \"%s\"",
getKey(), getValue());
}
System.out.println(Status.ACTIVE);
Prints
"key": Active",
"value": "1"
</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("AB", "AB description"),
CD("CD", "CD description"),
EF("EF", "EF description");
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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论