-1

I want to check if all objects of a stream meet a rule, and returns True only if all of them meets the rule, but I have a compilation error: Role cannot be applied to lambda parameter

public static Predicate<Hostel> areAllTrue() {
   return req -> req.getRole().stream(r -> isTrue(r));
}


private static boolean isTrue(HostelRole hostelRole) {

}
Sandro Rey
  • 2,429
  • 13
  • 36
  • 80
  • 1
    `stream(r -> isTrue(r))`, wouldn't compile since it doesn't accept an argument either. Possible duplicate of [Is it possible to check whether all Java 8 stream elements satify one of given predicates?](https://stackoverflow.com/questions/38903844/is-it-possible-to-check-whether-all-java-8-stream-elements-satify-one-of-given-p) – Naman Oct 16 '19 at 08:37

1 Answers1

2

Use the terminal operation allMatch:

public static Predicate<Hostel> areAllTrue() {
   return req -> req.getRole().stream().allMatch(r -> isTrue(r));
}
Eran
  • 387,369
  • 54
  • 702
  • 768