Reputation: 299
I have 2 lists:
List<String> authorizedList ; // ["11","22","33"]
List<MySerial> serials; // [Myserial1,Myserial2,Myserial3] =>
MySerial are object with 2 parameters, Myserial(String serial, String name)
I want to check if all serial from serials are in authorizedList using stream but I'm new using it.
if (!serials.stream().map(MySerial::getSerial).anyMatch(authorizedList::equals)) {
throw new UnauthorizedException();
}
But it always throw exception.
Upvotes: 0
Views: 1011
Reputation: 3735
Change authorizedList::equals
to authorizedList::contains
.
Also you can invert the if statement from anyMatch
to allMatch
.
E.g. after the suggestions you should have:
if (!serials.stream().map(MySerial::getSerial).allMatch(authorizedList::contains)) {
throw new UnauthorizedException();
}
Upvotes: 5