获取Java中字符串列表中的枚举名称

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

Getting the enum names in a list of string in Java

问题

我有一个枚举类我想从中获取名称列表并存储在一个字符串中然而为了做到这一点我实际上还在维护枚举的值尽管它们似乎只是重复的名称

public enum Forms {
    APPLICATION("APPLICATION"),
    VALIDATION("VALIDATION"),
    TARGETS("TARGETS");

    private final String value;

    Forms(String value) {
        this.value = value;
    }

    public static List<String> names() {
        return Stream.of(Forms.values())
                .map(Enum::name)
                .collect(Collectors.toList());
    }
}

由于名称和值在技术上是相同的我想知道是否有更好的方法来实现这一点由于我将在多个地方使用此列表因此我更喜欢从枚举类本身获取列表非常感谢任何帮助
英文:

I have an Enum from which I would like to get the list of names in a string. However to do this, I'm, maintaining the values of the enum as well though they seem to be just duplicated names.

  public enum Forms {
   APPLICATION(&quot;APPLICATION&quot;),
   VALIDATION(&quot;VALIDATION&quot;),
   TARGETS(&quot;TARGETS&quot;);

   private final String value;

   Forms(String value) {
       this.value = value;
   }

   public static List&lt;String&gt; names() {
    return Stream.of(Forms.values())
            .map(Enum::name)
            .collect(Collectors.toList());
   }
}

As the names and the values are technically the same, I'm wondering if there's a better way to achieve this? I'll be using this list in multiple places, therefore I prefer fetching the list from the Enum class itself. Any help would be much appreciated.

答案1

得分: 1

将映射更改为 `Forms::toString`

public static List<String> names() {
    return Stream.of(Forms.values())
        .map(Forms::toString)
        .collect(Collectors.toList());
}

如果您想要使用该值变量的不好的名称):

public static List<String> names() {
    return Stream.of(Forms.values())
        .map(Forms::getValue)
        .collect(Collectors.toList());
}
public String getValue() {
    return this.value;
}
英文:

Change the map to Forms::toString

   public static List &lt; String &gt; names() {
       return Stream.of(Forms.values())
           .map(Forms::toString)
           .collect(Collectors.toList());
   }

And if you want to use the value (bad name for a variable):

   public static List &lt; String &gt; names() {
       return Stream.of(Forms.values())
           .map(Forms::getValue)
           .collect(Collectors.toList());
   }
   public String getValue() {
       return this.value;
   }

答案2

得分: 0

默认情况下,Enum::name 返回枚举在源代码中定义的文本值。然而,值得注意的是JDK文档建议使用Enum::toString,因为它通常包含一个更易读的值(默认实现返回与名称方法相同的值)。正如f1sh指出的,你在任何地方都没有使用value,所以除非有更多未显示的代码,否则该字段是无用的。

另外,每次调用names时,你都在重新构建列表,我建议将其存储在一个静态的最终变量中,如下所示:

public static List<String> names() {
    return sNames;
}

private static final List<String> sNames = Collections.unmodifiableList(
      Stream.of(Forms.values())
         .map(Enum::name)
         .collect(Collectors.toList())));
英文:

By default, Enum::name returns the textual value of the enumeration as defined in the source code. However, it's worth noting that the JDK documentation suggests using Enum::toString instead, as it typically contains a more human-readable value (default implementation returns the same as the name method). As f1sh pointed out, you're never using value anywhere, so the field is useless unless there is more code that you aren't showing us.

As an aside, you're reconstructing the list every single time you invoke names, I'd suggest storing it in a static final variable, like so:

public static List&lt;String&gt; names() {
    return sNames;
}

private static final List&lt;String&gt; sNames = Collections.unmodifiableList(
      Stream.of(Forms.values())
         .map(Enum::name)
         .collect(Collectors.toList())));

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

发表评论

匿名网友

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

确定