如何在Java中使用枚举键值

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

How to use enum key value in java

问题

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

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

  1. public enum Status {
  2. ACTIVE("Active", 1), INACTIVE("Inactive", 2);
  3. private final String key;
  4. private final Integer value;
  5. Status(String key, Integer value) {
  6. this.key = key;
  7. this.value = value;
  8. }
  9. public String getKey() {
  10. return key;
  11. }
  12. public Integer getValue() {
  13. return value;
  14. }
  15. }

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

  1. [
  2. "ACTIVE",
  3. "INACTIVE"
  4. ]

但我想要得到如下结果:

  1. [
  2. {
  3. "Key": "Inactive",
  4. "value":"2"
  5. },
  6. {
  7. "Key": "Active",
  8. "value":"1"
  9. }
  10. ]

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

英文:

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

  1. public enum status{
  2. ACTIVE("Active", 1), IN_ACTIVE("In Active", 2);
  3. private final String key;
  4. private final Integer value;
  5. Status(String key, Integer value) {
  6. this.key = key;
  7. this.value = value;
  8. }
  9. public String getKey() {
  10. return key;
  11. }
  12. public Integer getValue() {
  13. return value;
  14. }
  15. }

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

  1. [
  2. "ACTIVE",
  3. "INACTIVE"
  4. ]

But i want to got like this

  1. [
  2. {
  3. "Key": "Inactive",
  4. "value":"2"
  5. },
  6. {
  7. "Key": "Active",
  8. "value":"1"
  9. }
  10. ]

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

答案1

得分: 3

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

  1. enum Status {
  2. ACTIVE("Active", 1), IN_ACTIVE("In Active", 2);
  3. private final String key;
  4. private final int value;
  5. Status(String key, int value) {
  6. this.key = key;
  7. this.value = value;
  8. }
  9. public String getKey() {
  10. return key;
  11. }
  12. public int getValue() {
  13. return value;
  14. }
  15. public Entry<String,Integer> getBoth() {
  16. return new AbstractMap.SimpleEntry<>(key, value);
  17. }
  18. }
  19. Entry<String,Integer> e = Status.ACTIVE.getBoth();
  20. System.out.println("Key: = " + e.getKey());
  21. System.out.println("Value: = " + e.getValue());

或者打印Entry的toString()值。

  1. System.out.println(e);

输出

  1. Key: = Active
  2. Value: = 1
  3. Active=1

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

  1. public String toString() {
  2. return String.format("\"key\": \"%s\",\n\"value\": \"%s\"",
  3. getKey(), getValue());
  4. }
  5. System.out.println(Status.ACTIVE);

输出

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

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

  1. enum Status {
  2. ACTIVE(&quot;Active&quot;, 1), IN_ACTIVE(&quot;In Active&quot;, 2);
  3. private final String key;
  4. private final int value;
  5. Status(String key, int value) {
  6. this.key = key;
  7. this.value = value;
  8. }
  9. public String getKey() {
  10. return key;
  11. }
  12. public int getValue() {
  13. return value;
  14. }
  15. public Entry&lt;String,Integer&gt; getBoth() {
  16. return new AbstractMap.SimpleEntry&lt;&gt;(key, value);
  17. }
  18. }
  19. Entry&lt;String,Integer&gt; e = Status.ACTIVE.getBoth();
  20. System.out.println(&quot;Key: = &quot; + e.getKey());
  21. System.out.println(&quot;Value: = &quot; + e.getValue());

or print the toString() value of the Entry.

  1. System.out.println(e);

Prints

  1. Key: = Active
  2. Value: = 1
  3. Active=1

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

  1. public String toString() {
  2. return String.format(&quot;\&quot;key\&quot;: \&quot;%s\&quot;,%n\&quot;value\&quot;: \&quot;%s\&quot;&quot;,
  3. getKey(), getValue());
  4. }
  5. System.out.println(Status.ACTIVE);

Prints

  1. &quot;key&quot;: Active&quot;,
  2. &quot;value&quot;: &quot;1&quot;
  3. </details>
  4. # 答案2
  5. **得分**: 0
  6. ```java
  7. public enum FileCode {
  8. AB("AB", "AB 描述"),
  9. CD("CD", "CD 描述"),
  10. EF("EF", "EF 描述");
  11. private final String key;
  12. private final String value;
  13. private final KeyValue keyValue;
  14. FileCode(String key, String value) {
  15. this.key = key;
  16. this.value = value;
  17. this.keyValue = new KeyValue(key, value);
  18. }
  19. public static List<KeyValue> getAllKeyValue() {
  20. List<KeyValue> keyValueList = new ArrayList<>();
  21. for (FileCode f : FileCode.values()) {
  22. keyValueList.add(KeyValue.builder()
  23. .key(f.key)
  24. .value(f.value)
  25. .build());
  26. }
  27. return keyValueList;
  28. }
  29. public Map.Entry<String, String> getBoth() {
  30. return new AbstractMap.SimpleEntry<>(key, value);
  31. }
  32. @Data
  33. @NoArgsConstructor
  34. @AllArgsConstructor
  35. @Builder
  36. public static class KeyValue {
  37. String key;
  38. String value;
  39. }
  40. }
英文:

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

  1. public enum FileCode {
  2. AB(&quot;AB&quot;, &quot;AB description&quot;),
  3. CD(&quot;CD&quot;, &quot;CD description&quot;),
  4. EF(&quot;EF&quot;, &quot;EF description&quot;);
  5. private final String key;
  6. private final String value;
  7. private final KeyValue keyValue;
  8. FileCode(String key, String value) {
  9. this.key = key;
  10. this.value = value;
  11. this.keyValue = new KeyValue(key, value);
  12. }
  13. public static List&lt;KeyValue&gt; getAllKeyValue() {
  14. List&lt;KeyValue&gt; keyValueList = new ArrayList&lt;&gt;();
  15. for (FileCode f : FileCode.values()) {
  16. keyValueList.add(KeyValue.builder()
  17. .key(f.key)
  18. .value(f.value)
  19. .build());
  20. }
  21. return keyValueList;
  22. }
  23. public Map.Entry&lt;String, String&gt; getBoth() {
  24. return new AbstractMap.SimpleEntry&lt;&gt;(key, value);
  25. }
  26. @Data
  27. @NoArgsConstructor
  28. @AllArgsConstructor
  29. @Builder
  30. public static class KeyValue {
  31. String key;
  32. String value;
  33. }
  34. }

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:

确定