Change the Way You Think

Imperative style—that’s what Java has provided us with since its inception. In this style, we tell Java every step of what we want it to do and then we watch it faithfully exercise those steps. That’s worked fine, but it’s a bit low-level. The code tends to get verbose, and we often wish the language were a tad more intelligent; we could then tell it—declaratively—what we want rather than delve into how to do it. Thankfully, Java can help us do that. Let’s look at a few examples to see the benefits and the differences in style.

The Habitual Way

Let’s start on familiar ground to see the two paradigms in action. Here’s an imperative way to find if Chicago is in a collection of given cities—remember, the listings in this ...

Get Functional Programming in Java, 2nd 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.