Java Reference
In-Depth Information
This particular example is explained in detail in chapter 6 , “Collecting data with streams.” It
basically groups dishes by their types inside a Map. For example, the Map may contain the
following result:
{FISH=[prawns, salmon],
OTHER=[french fries, rice, season fruit, pizza],
MEAT=[pork, beef, chicken]}
Now try to think how you'd implement this with the typical imperative programming approach
using loops. Don't waste too much of your time. Embrace the power of streams in this and the
following chapters!
Other libraries: Guava, Apache, and lambdaj
There have been many attempts at providing Java programmers with better libraries to
manipulate collections. For example, Guava is a popular library created by Google. It provides
additional container classes such as multimaps and multisets. The Apache Commons Collections
library provides similar features. Finally, lambdaj, written by Mario Fusco, coauthor of this topic,
provides many utilities to manipulate collections in a declarative manner, inspired by functional
programming.
Now Java 8 comes with its own official library for manipulating collections in a more declarative
style.
To summarize, the Streams API in Java 8 lets you write code that's
Declarative More concise and readable
Composable Greater flexibility
Parallelizable Better performance
For the remainder of this chapter and the next, we'll use the following domain for our examples:
a menu that's nothing more than a list of dishes
List<Dish> menu = Arrays.asList(
new Dish("pork", false, 800, Dish.Type.MEAT),
new Dish("beef", false, 700, Dish.Type.MEAT),
new Dish("chicken", false, 400, Dish.Type.MEAT),
Search WWH ::




Custom Search