使用流检查 List<String> 是否包含 List<Object> 中的所有元素
Check if a List<String> contains all element from a List<Object> using stream
我有 2 个列表:
List<String> authorizedList ; // ["11","22","33"]
List<MySerial> serials; // [Myserial1,Myserial2,Myserial3] =>
MySerial 是具有 2 个参数的对象,Myserial(String serial, String name)
我想使用流检查连续剧中的所有连续剧是否都在 authorizedList 中,但我是新手。
if (!serials.stream().map(MySerial::getSerial).anyMatch(authorizedList::equals)) {
throw new UnauthorizedException();
}
但它总是抛出异常。
将 authorizedList::equals
更改为 authorizedList::contains
。
您还可以将 if 语句从 anyMatch
反转为 allMatch
。
例如在你应该有的建议之后:
if (!serials.stream().map(MySerial::getSerial).allMatch(authorizedList::contains)) {
throw new UnauthorizedException();
}
我有 2 个列表:
List<String> authorizedList ; // ["11","22","33"]
List<MySerial> serials; // [Myserial1,Myserial2,Myserial3] =>
MySerial 是具有 2 个参数的对象,Myserial(String serial, String name)
我想使用流检查连续剧中的所有连续剧是否都在 authorizedList 中,但我是新手。
if (!serials.stream().map(MySerial::getSerial).anyMatch(authorizedList::equals)) {
throw new UnauthorizedException();
}
但它总是抛出异常。
将 authorizedList::equals
更改为 authorizedList::contains
。
您还可以将 if 语句从 anyMatch
反转为 allMatch
。
例如在你应该有的建议之后:
if (!serials.stream().map(MySerial::getSerial).allMatch(authorizedList::contains)) {
throw new UnauthorizedException();
}