使用Java 8默认方法和实用程序类的Java通用枚举功能

使用Java 8默认方法和实用程序类的Java通用枚举功能,java,generics,enums,interface,java-8,Java,Generics,Enums,Interface,Java 8,下面是我提出的失败的尝试,指的是 我想找到一个更好的方法。我发现这种方法存在两个问题: 每次我都需要通过课程类型。示例-EnumUtility.fromKey(Country.class,1) fromSet在城市和国家都是重复的 ‌ 公共枚举城市实现BaseEnumInterface{ 东京(0)、纽约(1); 私钥; 公共静态集合fromValue(集合枚举){ 返回enums.stream().map(City::getKey).collect(Collectors.toSet());

下面是我提出的失败的尝试,指的是

我想找到一个更好的方法。我发现这种方法存在两个问题:

  • 每次我都需要通过课程类型。示例-EnumUtility.fromKey(Country.class,1)

  • fromSet在城市和国家都是重复的

公共枚举城市实现BaseEnumInterface{
东京(0)、纽约(1);
私钥;
公共静态集合fromValue(集合枚举){
返回enums.stream().map(City::getKey).collect(Collectors.toSet());
}
public int getKey(){
返回键;
}
私人城市(国际密钥){
this.key=key;
}
}
公共枚举国家/地区实现BaseEnumInterface{
美国(0),英国(1),;
私钥;
公共静态集合fromSet(集合枚举){
返回enums.stream().map(Country::getKey).collect(Collectors.toSet());
}
public int getKey(){
返回键;
}
私人国家(国际密钥){
this.key=key;
}
}
公共类枚举实用程序{
公共静态E fromKey(类enumClass,整数键){
对于(E类型:enumClass.getEnumConstants()){
if(key==type.getKey()){
返回类型;
}
}
抛出新的IllegalArgumentException(“提供的枚举类型无效”);
}
公共静态集合fromSet(类enumClass,集enums){
返回enums.stream().map(BaseEnumInterface::getKey).collect(Collectors.toSet());
}   
}
接口BaseEnumInterface{
int getKey();
}
公共类枚举测试器{
公共静态void main(字符串参数[]){
System.out.println(enumputility.fromKey(Country.class,1));
}
}

无法避免将enum类传递给fromKey。否则,您如何知道要检查哪些枚举常量以获取请求的密钥?注意:该方法中的第二个参数的类型应为
int
,而不是整数。在整数实例上使用
=
不会比较数值,它会比较对象引用


EnumUtility.fromSet应该可以正常工作,因此每个enum类根本不需要fromSet方法。请注意,EnumUtility.fromSet根本不需要类参数,事实上您的代码没有使用该参数。

那么为什么您的
enum
s再次实现接口?好的,每个类城市和国家的fromValue呢?我可以避免重复吗?City.fromValue和Country.fromSet是相同的。当我引用每个枚举类的fromSet方法时,我同时引用了City.fromValue和Country.fromSet。这两种方法都不需要。不仅
Class
参数已过时。您可以通过
publicstaticset fromSet(Set
public enum City implements BaseEnumInterface {

    TOKYO(0), NEWYORK(1);

    private final int key;

    public static Set<Integer> fromValue(Set<City> enums) {
        return enums.stream().map(City::getKey).collect(Collectors.toSet());
    }

    public int getKey() {
        return key;
    }

    private City(int key) {
        this.key = key;
    }
}


public enum Country implements BaseEnumInterface {

    USA(0), UK(1);

    private final int key;

    public static Set<Integer> fromSet(Set<Country> enums) {
        return enums.stream().map(Country::getKey).collect(Collectors.toSet());
    }

    public int getKey() {
        return key;
    }

    private Country(int key) {
        this.key = key;
    }

}


public class EnumUtility {

    public static <E extends Enum<E> & BaseEnumInterface> E fromKey(Class<E> enumClass, Integer key) {
        for (E type : enumClass.getEnumConstants()) {
            if (key == type.getKey()) {
                return type;
            }
        }
        throw new IllegalArgumentException("Invalid enum type supplied");
    }

    public static <E extends Enum<E> & BaseEnumInterface> Set<Integer> fromSet(Class<E> enumClass, Set<E> enums) {
        return enums.stream().map(BaseEnumInterface::getKey).collect(Collectors.toSet());
    }   
}


interface BaseEnumInterface {

    int getKey();

}


public class EnumTester {

    public static void main(String args[]) {
        System.out.println(EnumUtility.fromKey(Country.class, 1));
    }
}