Java 检查枚举是否包含列表中的所有值<;字符串>;

Java 检查枚举是否包含列表中的所有值<;字符串>;,java,java-8,enums,Java,Java 8,Enums,我想检查字符串列表是否包含枚举中不存在的任何值 public interface MyInterface { public static enum MyEnum { ONE, TWO, THREE, FOUR; } public otherMethodsBelow(); } 下面是我的API,用户将在其中传递字符串列表 @GET @Path("/{path}") public Response find(@QueryParam(&quo

我想检查字符串列表是否包含枚举中不存在的任何值

public interface MyInterface {

public static enum MyEnum { 
    ONE,
    TWO,
    THREE,
    FOUR;
}

public otherMethodsBelow();
}

下面是我的API,用户将在其中传递字符串列表

@GET
@Path("/{path}")
public Response find(@QueryParam("list") final List<String> list) {
if(list contains the values that are present in MyEnum){ //if list is "ONE","THREE" then go inside if
    // do stuff
}else if(list contains any other value that is not present in MyEnum){ //if list is "ONE","FIVE" then go inside else if
    throw "Invalid Arguement in list"
}}
@GET
@路径(“/{Path}”)
公共响应查找(@QueryParam(“列表”)最终列表){
如果(列表包含MyEnum中存在的值){//如果列表为“一”,“三”,则进入if
//做事
}elseif(列表包含MyEnum中不存在的任何其他值){//如果列表为“一”,“五”,则进入else if
抛出“列表中的无效参数”
}}

我的要求是,如果列表包含任何错误的值(MyEnum中没有的值),那么它应该放在else if块中,如果列表包含MyEnum中也存在的值,那么它应该放在if块中。

使用流,您可以将
MyEnum
常量映射到一组字符串

private static final Set<String> ALL_MY_ENUMS = Arrays.stream(MyEnum.values()).map(Enum::name).collect(toSet());

@GET
@Path("/{path}")
public Response find(@QueryParam("list") final List<String> list) {
    List<String> extraValues = new ArrayList<>(list);
    extraValues.removeAll(ALL_MY_ENUMS);
    if (extraValues.isEmpty()) {
        // do stuff
    } else {
        throw "Invalid Arguement in list"
    }
private static final Set ALL_MY_ENUMS=Arrays.stream(MyEnum.values()).map(Enum::name).collect(toSet());
@得到
@路径(“/{Path}”)
公共响应查找(@QueryParam(“列表”)最终列表){
List extraValues=新的ArrayList(列表);
extraValues.removeAll(所有我的枚举);
if(extraValues.isEmpty()){
//做事
}否则{
抛出“列表中的无效参数”
}
//缓存要设置的枚举字符串值(恒定时间访问时间)
私有静态最终集myEnumStringValues=Arrays.stream(MyEnum.values()).map(Enum::name).collect(Collectors.toSet());
@得到
@路径(“/{Path}”)
公共响应查找(@QueryParam(“列表”)最终列表){
//O(N),其中N=最大sizeof(MyEnumStringValue)
if(myEnumStringValues.containsAll(list)){//如果list是“一”,“三”,那么进入if
//做事
}else{//如果列表是“一”,“五”,那么进入else如果
抛出“列表中的无效参数”//您可以创建列表的副本并调用removeAll(myEnumStringValues)->这并不总是需要的,我想通常我会有一个有效的案例,所以只有在失败时才进行该操作更便宜
}
}

您可以按照@Andy Turner的建议或

Set<String> set = new HashSet<>(list);
Set<String> enumSet = Arrays.stream(RequestType.values()).map(Enum::name).collect(Collectors.toSet()));
    if (set.equals(enumSet)) {

    } else {

    }
Set Set=newhashset(列表);
Set enumSet=Arrays.stream(RequestType.values()).map(Enum::name).collect(Collectors.toSet());
if(set.equals(enumSet)){
}否则{
}

添加一个方法
私有布尔hasValidEnums(List listOfEnums)
并检查列表中的每个内容,看看它们是否实际有效?(Stream+filter不在enum+findAny+isPresent中)?
List.containsAll(Arrays.Stream(MyEnum.values()).map(enum::name).collect(toSet())
为什么您的伪代码包含两个if语句?这样,您就有三个可能的条件。不清楚您真正想要测试哪一个。
if (!contains(list))
// cache enum string values to set (constant time access time)
private static final Set<String> myEnumStringValues = Arrays.stream(MyEnum.values()).map(Enum::name).collect(Collectors.toSet());

@GET
@Path("/{path}")
public Response find(@QueryParam("list") final List<String> list) {
    // O(N), where N = at most sizeof(myEnumStringValues)
    if(myEnumStringValues.containsAll(list)){ //if list is "ONE","THREE" then go inside if
        // do stuff
    }else { //if list is "ONE","FIVE" then go inside else if
        throw "Invalid Arguement in list" // you can create a copy of list and call removeAll(myEnumStringValues) -> this is not always needed, I guess usually I'll have a valid case, so it is cheaper to do that operation only if it fails
    }
}
    
Set<String> set = new HashSet<>(list);
Set<String> enumSet = Arrays.stream(RequestType.values()).map(Enum::name).collect(Collectors.toSet()));
    if (set.equals(enumSet)) {

    } else {

    }