Nothing special here, same changes as formerly, but we are using people.isEmpty()
instead of people.size() == 0
for even better legibility.
package chapter11; import org.junit.jupiter.api.Test; import java.util.Set; import static org.junit.jupiter.api.Assertions.*; public class ForEachTest { record Person(String name, int age) { } interface Agency { boolean isChaperoneRequired(Set<Person> people); } static class AgencyBefore implements Agency { public boolean isChaperoneRequired(Set<Person> people) { boolean required = true; if (people.isEmpty()) { required = false; } else { for (var person : people) { if (person.age() >= 18) { required = false; break; } } } return required; } } static class AgencyAfter implements Agency { public boolean isChaperoneRequired(Set<Person> people) { return !people.isEmpty() && people.stream() .noneMatch(person -> person.age() >= 18); } } private static void commonAgencyTests(Agency agency) { assertAll( () -> assertTrue(agency.isChaperoneRequired( Set.of(new Person("Jake", 12)))), () -> assertTrue(agency.isChaperoneRequired( Set.of(new Person("Jake", 12), new Person("Pam", 14)))), () -> assertTrue(agency.isChaperoneRequired( Set.of(new Person("Shiv", 8), new Person("Sam", 9), new Person("Jill", 11)))), () -> assertFalse(agency.isChaperoneRequired( Set.of(new Person("Jake", 12), new Person("Pam", 18)))), () -> assertFalse(agency.isChaperoneRequired(Set.of())) ); } @Test void agencyBefore() { commonAgencyTests(new AgencyBefore()); } @Test void agencyAfter() { commonAgencyTests(new AgencyAfter()); } }