O'Reilly logo

Java All-in-One For Dummies, 4th Edition by Doug Lowe

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 6

Using Bulk Data Operations with Collections

In This Chapter

arrow Understanding basic stream operations

arrow Examining the stream interface

arrow Filtering and sorting streams

arrow 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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required