July 2023
Intermediate to advanced
276 pages
6h 55m
English
A common question I hear from developers is "should I write multiple filters with one condition each or one filter with multiple conditions?" The short and quick answer is to use multiple filter calls with one condition each.
Consider the following code:
| | drivers.stream() |
| | .filter(driver -> driver.getAge() > 21 && driver.isDriversLicenseValid()) |
| | //Please don't code like the previous line |
| | .map(driver -> driver.getPrimaryCar()) |
| | .map(car -> car.getRegistration()) |
| | .forEach(registration -> System.out.println(registration)); |
The lambda expression passed to the filter method does two separate checks: if the driver is more than 21 years old and if the driver has a valid driver’s license. Two conditions ...
Read now
Unlock full access