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. Wouldn’t ...
Get Java All-in-One For Dummies, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.