The previous recipe described how to generate the two types of streams, but did not mention more about parallel streams. This final recipe will explore the pros and cons of using parallel streams.
Open project ch06
again to add service methods that will compare and contrast the two types of streams, sequential and parallel streams. The services will use the same EmployeeDao
for the JDBC transactions.
All the preceding recipes highlighted the sequential stream which is common to many stream-based transactions. Let us now generate the parallel stream form by following these steps:
- Create a service class,
EmployeeParallelStreamService
, inside the same package as the previous service classes. Add the following version ofshowAllEmployees()
that usesparallelStream()
toforEach()
all employee records:
public void showAllEmployees(){ Consumer<Employee> showAll = (e) -> { System.out.format("%s %s %d\n", e.getFirstName...