Chapter 11. Functional Approaches in Java

Java is—and remains—an object-oriented language. All of the design patterns and class types we saw in Chapter 5 are still core to how most developers write Java code. Java is also flexible, with individual and corporate contributors proposing and making improvements. As functional programming (FP) moves back into the limelight, Java is keeping up. FP represents an alternate way to approach programming: functions, rather than objects, are the focus.

Starting as far back as Java 8, Java has supported a reasonable set of functional features with the java.util.function package. This package includes several classes and interfaces that allow developers to use popular functional approaches to problem-solving. We’ll explore some of these approaches in this chapter, but we want to emphasize that verb, allow. If you don’t enjoy functional programming, you can safely ignore this chapter. We hope you’ll try some of the examples, though. There are some nice features that can make your code more compact while retaining its readability.

Functions 101

The roots of functional programming extend all the way back to the 1930s, with American mathematician Alonzo Church and his lambda calculus. Church wasn’t running his calculus on any hardware, but lambda calculus formalized a way of problem-solving that would lead to early programming languages written for real, operating machines.1 The Lisp language was developed in the 1950s at MIT and ran on early iterations ...

Get Learning Java, 6th 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.