Determine if any members in a collection have a boolean value set to true

How can you check a collection to see if any of a particular field are set to a value.

  • I have a composite entity with a boolean field.
  • I need to check if any value has been set to true
  • If so, there is another boolean field on the owning entity that also must be set to true.

I have a collection datasource listener set up to trigger a function:

employersDs.addCollectionChangeListener(event -> statusSetter());`

...

private void statusSetter() {
 //check if any employer hired referral
 Boolean wasHired = employersDs.getItems().stream().anyMatch(employersDs.getItem().getHired() == true);
 //if yes, set jobSeekerHired to Yes
 if (wasHired) {
  getItem().setJobSeekerHired(YesNoOTher.Yes);
 }
}

I just can’t get my hands around the syntax to achieve this. Any help would be appreciated.

Haven’t run the code, but I think you need something like this:

employersDs.addCollectionChangeListener(event -> statusSetter());

...

private void statusSetter() {
 //check if any employer hired referral
 Boolean wasHired = employersDs.getItems().stream().anyMatch(employer -> employer.getHired() == true);
 //if yes, set jobSeekerHired to Yes
 if (wasHired) {
  getItem().setJobSeekerHired(YesNoOTher.Yes);
 }
}

anyMatch will give you the parameter to work with (in this case, I’ve called in employer), and then you supply the code that determines what constitutes a match.

1 Like

Thanks! I this worked great.