Chapter 6
Using Bulk Data Operations with Collections
In This Chapter
Understanding basic stream operations
Examining the stream interface
Filtering and sorting streams
Computing sums, averages, and other values
One of the most common things to do with a collection is to iterate over it, performing some type of operation on all of its elements. For example, you might use a foreach loop to print all of the elements. The body of the foreach loop might contain an if statement to select which elements to print. Or it might perform a calculation such as accumulating a grand total or counting the number of elements that meet a given condition.
In Java, foreach loops are easy to create and can be very powerful. However, they have one significant drawback: They iterate over the collection's elements one at a time, beginning with the first element and proceeding sequentially to the last element. As a result, a foreach loop must be executed sequentially within a single tread.
That's a shame, since modern computers have multicore processors that are capable of doing several things at once. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access