enum values() - 每次调用或分配给变量
enum values() - call each time or assign to variable
来自阅读 Enum Types, The Java Tutorials:
The compiler automatically adds some special methods when it creates
an enum. For example, they have a static values method that returns an
array containing all of the values of the enum in the order they are
declared. This method is commonly used in combination with the
for-each construct to iterate over the values of an enum type.
我想知道 values()
方法到底做了什么。它实际上只是返回一个 class 成员/常量,还是更多地在幕后进行?以下代码绝不 旨在展示最佳实践,但强调了这个问题的答案将如何影响我处理问题的方式:
如果 values()
returns 一个 class 成员/常量我会考虑自由地调用它(参见 getAllValues()
),像这样:
enum ExampleEnum {
A("A's String"), B("B's String"), C("C's String");
ExampleEnum(String someValue) {
this.someValue = someValue;
}
private final String someValue;
private String[] getAllValues() {
String[] result = new String[values().length];
for (int i = 0; i < values().length; i++) {
result[i] = values()[i].someValue;
}
return result;
}
}
如果它确实在做实际工作,我会按如下方式更改 getAllValues()
:
private String[] getAllValues() {
ExampleEnum[] values = values();
String[] result = new String[values.length];
for (int i = 0; i < values.length; i++) {
result[i] = values[i].someValue;
}
return result;
}
每次调用 values()
时,您都在创建新的数组,其中填充了枚举常量。您可以使用 ==
operator
轻松测试它
System.out.println(ExampleEnum.values()==ExampleEnum.values());//prints false
所以第二个选项会更好,因为它创建了这个数组一次。但是要改进它,您可以简单地将 values()
的结果存储在此方法之外,并在需要时重用它。这样你就可以简单地调用它一次。您还可以更早地创建 someValue
个元素的集合,将其存储为不可修改的集合(如 List)并 return 它。
因此您的代码可能如下所示:
enum ExampleEnum {
A("A's String"), B("B's String"), C("C's String");
ExampleEnum(String someValue) {
this.someValue = someValue;
}
private final String someValue;
private static final ExampleEnum[] vals = values();
private static final List<String> names = Collections.unmodifiableList(
Stream.of(vals).map(en -> en.someValue).collect(Collectors.toList()));
public static List<String> getAllValues() {
return names;
}
}
来自阅读 Enum Types, The Java Tutorials:
The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared. This method is commonly used in combination with the for-each construct to iterate over the values of an enum type.
我想知道 values()
方法到底做了什么。它实际上只是返回一个 class 成员/常量,还是更多地在幕后进行?以下代码绝不 旨在展示最佳实践,但强调了这个问题的答案将如何影响我处理问题的方式:
如果 values()
returns 一个 class 成员/常量我会考虑自由地调用它(参见 getAllValues()
),像这样:
enum ExampleEnum {
A("A's String"), B("B's String"), C("C's String");
ExampleEnum(String someValue) {
this.someValue = someValue;
}
private final String someValue;
private String[] getAllValues() {
String[] result = new String[values().length];
for (int i = 0; i < values().length; i++) {
result[i] = values()[i].someValue;
}
return result;
}
}
如果它确实在做实际工作,我会按如下方式更改 getAllValues()
:
private String[] getAllValues() {
ExampleEnum[] values = values();
String[] result = new String[values.length];
for (int i = 0; i < values.length; i++) {
result[i] = values[i].someValue;
}
return result;
}
每次调用 values()
时,您都在创建新的数组,其中填充了枚举常量。您可以使用 ==
operator
System.out.println(ExampleEnum.values()==ExampleEnum.values());//prints false
所以第二个选项会更好,因为它创建了这个数组一次。但是要改进它,您可以简单地将 values()
的结果存储在此方法之外,并在需要时重用它。这样你就可以简单地调用它一次。您还可以更早地创建 someValue
个元素的集合,将其存储为不可修改的集合(如 List)并 return 它。
因此您的代码可能如下所示:
enum ExampleEnum {
A("A's String"), B("B's String"), C("C's String");
ExampleEnum(String someValue) {
this.someValue = someValue;
}
private final String someValue;
private static final ExampleEnum[] vals = values();
private static final List<String> names = Collections.unmodifiableList(
Stream.of(vals).map(en -> en.someValue).collect(Collectors.toList()));
public static List<String> getAllValues() {
return names;
}
}