Chapter 6

Using Bulk Data Operations with Collections


check Understanding basic stream operations

check Examining the stream interface

check Filtering and sorting streams

check 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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.