2 回答
TA贡献1812条经验 获得超5个赞
这是计算交集的简单方法(更改输入以使结果明显):
List<String> properties = Arrays.asList(new String[] { "E1", "E2" });
List<MyEnumClass> enums = Arrays.asList(new MyEnumClass[]
{ MyEnumClass.E2, MyEnumClass.E3 });
List<String> enumNames = new ArrayList<>();
for (MyEnumClass e : enums)
enumNames.add(e.name());
List<String> intersection = new ArrayList<>(enumNames);
intersection.retainAll(properties);
System.out.println(intersection); //[E2]
这可以计算彼此之间的差异:
List<String> propDiff = new ArrayList<>(properties);
propDiff.removeAll(enumNames);
System.out.println(propDiff); //[E1]
List<String> enumDiff = new ArrayList<>(enumNames);
enumDiff.removeAll(properties);
System.out.println(enumDiff); //[E3]
TA贡献1784条经验 获得超7个赞
你可以这样使用EnumSet<MyEnumClass>:
Set<MyEnumClass> all = EnumSet.allOf(MyEnumClass.class);
EnumSet<MyEnumClass>然后从您的属性创建另一个:
Set<MyEnumClass> loaded = EnumSet.noneOf(MyEnumClass.class);
for(String property : properties) {
loaded.add(MyEnumClass.valueOf(property));
}
Collection.removeAll然后,您可以使用, Collection.addAll,来检查不同的内容Collection.retainAll:
// all will now contain every element from loaded
all.addAll(loaded);
// all will now contain every element which was not contained in loaded
all.removeAll(loaded);
// all will now contain every element which was contained in loaded
all.retainAll(loaded);
添加回答
举报