英文:
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("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());
}
}
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 < String > 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 < String > 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<String> names() {
return sNames;
}
private static final List<String> sNames = Collections.unmodifiableList(
Stream.of(Forms.values())
.map(Enum::name)
.collect(Collectors.toList())));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论