重构 - 从枚举生成字符串列表
方法 getContinent()
需要一个 String
作为参数,该参数可以具有三个可能的值:"Africa"
、"Asia"和<代码>“欧洲”。
根据我得到的给定值,我必须匹配适当的枚举并获取其所有值的列表(作为字符串)。
我的代码中有重复。
有更好的方法吗?
将其放入每个 enum
中是个好主意吗?
Stream.of(CountryAsia.values())
.map(country -> country.getName())
.collect(Collectors.toList());
: 我的代码
Main
类:
public class Main {
public static void main(String[] args) {
String continent = getContinent() //one of three possible values: "Africa" or "Asia" or "Europe"
List<String> list = null;
switch(continent) {
case "Africa":
list = Stream.of(CountryAfrica.values())
.map(country -> country.getName())
.collect(Collectors.toList());
break;
case "Asia":
list = Stream.of(CountryAsia.values())
.map(country -> country.getName())
.collect(Collectors.toList());
break;
case "Europe":
list = Stream.of(CountryEurope.values())
.map(country -> country.getName())
.collect(Collectors.toList());
break;
}
System.out.println(list);
//next instructions..
}
}
CountryEurope
枚举:
public enum CountryEurope {
DENMARK("Denmark"),
ESTONIA("Estonia"),
ICELAND("Iceland");
CountryEurope(String name) {
this.name = name;
}
private final String name;
public String getName() {
return name;
}
}
CountryAsia
枚举:
public enum CountryAsia {
AFGHANISTAN("Afghanistan"),
KAZAKHSTAN("Kazakhstan"),
UNITED_ARAB_EMIRATES("United Arab Emirates"),
UZBEKISTAN("Uzbekistan");
CountryAsia(String name) {
this.name = name;
}
private final String name;
public String getName() {
return name;
}
}
CountryAfrica
类:
public enum CountryAfrica {
ALGIERIA("Algeria"),
WESTERN_SAHARA("Western Sahara"),
EGYPT("Egypt");
CountryAfrica(String name) {
this.name = name;
}
private final String name;
public String getName() {
return name;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
EnumSet.allOf()
以便获取enum
常量的名称。EnumSet.allOf()
需要一个enum
的Class
作为参数,并返回其EnumSet
元素(这是特殊的实现专门为enum
设计的Set
)。然后,我们可以使用流或纯文本将
enum
常量的Set
转换为字符串列表for
循环。如果您使用的是Java 14 +,则可以使用切换表达式。否则,用方法调用替换重复的流语句。
为了使用这些
enum
中的每个元素(无论其类型如何)访问方法getName()
,我们可以定义一个接口,其中所有他们将实施。这样它将作为所有国家/地区枚举的入口点。此外,它还可能包含一些与特定于域的信息相关的其他方法,这些信息可能在您的应用程序中有用,例如国家/地区或地区税率(所有这些行为都可以通过
CountryData
接口访问)。结合上面提到的所有内容将给出非常简洁且富有表现力的代码:
上面的方法声明了一个有界类型参数
; & CountryData>
,这意味着任何类型T
都是Enum
类和接口CountryData
。有关泛型方法语法的信息,请查看本教程
main()
输出
不,这并不是因为:
You can make use
EnumSet.allOf()
in order to get the names of yourenum
constants.EnumSet.allOf()
expects aClass<T>
ofenum
as an argument and returns anEnumSet
of its elements (which is special implementation of theSet
designed exclusively forenum
s).Then we can transform a
Set
ofenum
constants into a list of strings either by using a stream or a plainfor
loop.And if you are using Java 14 +, you cane utilize switch expressions. Otherwise, replace the repeating stream statement with a method call.
In order to access the method
getName()
with every element of theseenum
s regardless of its type, we can define an interface which all of them will implement. So that it'll serve as an entry-point to all country-enums.Also, it could contain some other methods related to the domain-specific information that could be useful in your application, like country area or locale tax rates (all this behavior will be accessible though the
CountryData
interface).Combining all mentioned above will give a very concise and expressive code:
Method above declares a bounded type parameter
<T extends Enum<T> & CountryData>
, which implies any typeT
which is subtype of bothEnum
class and interfaceCountryData
.for information on the syntax of generic methods, take a look at this tutorial
main()
Output
No, it's not because:
enum
s therefore it must reside in one place (in a class that is meant to process this list).看起来您尝试在枚举中存储一些数据集合,恕我直言,这有点错误。我更愿意提供一张地图,其中键是“大陆”,值是国家/地区的集合。映射<字符串,集合>。
然后,你不需要使用开关,你可以使用Map#get()。
Looks like you try to store some collection of data inside enum that is a bit wrong IMHO. I would prefer to provide a map where the key is "continent" and the value is a collection of countries. Map<String, Set>.
Then, you don't need to use the switch, you can use Map#get().