O'Reilly logo

Functional Programming for Java Developers by Dean Wampler

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


Welcome to Functional Programming for Java Developers

Why should a Java developer learn about functional programming (FP)? After all, hasn’t functional programming been safely hidden in academia for decades? Isn’t object-oriented programming (OOP) all we really need? This book explains why functional programming has become an important tool for the challenges of our time and how you, a Java developer, can use it to your advantage.

The recent interest in functional programming started as a response to the growing pervasiveness of concurrency as a way of scaling horizontally, through parallelism. Multithreaded programming (see, e.g., [Goetz2006]) is difficult to do well and few developers are good at it. As we’ll see, functional programming offers better strategies for writing robust, concurrent software.

An example of the greater need for horizontal scalability is the growth of massive data sets requiring management and analysis, the so-called big data trend. These are data sets that are too large for traditional database management systems. They require clusters of computers to store and process the data. Today, it’s not just Google, Yahoo!, Facebook, and Twitter who work with big data. Many organizations face this challenge.

Once you learn the benefits of functional programming, you find that it improves all the code you write. When I learned functional programming a few years ago, it re-energized my enthusiasm for programming. I saw new, exciting ways to approach old problems. The rigor of functional programming complemented the design and testing benefits of test-driven development, giving me greater confidence in my work. I learned functional programming using the Scala programming language [Scala] and co-wrote a book on Scala with Alex Payne, called Programming Scala (O’Reilly). Scala is a JVM language, a potential successor to Java, with the goal of bringing object-oriented and functional programming into one coherent whole. Clojure is the other well-known functional language on the JVM. It is a Lisp dialect that minimizes the use of OOP in favor of functional programming. Clojure embodies a powerful vision for how programming should be done.

Fortunately, you don’t have to adopt a new language to enjoy many of the benefits of functional programming. Back in early 1990s, I used an object-oriented approach in the C software I wrote, until I could use C++. Similarly, if you’re working with an object-oriented language, like Java, you can still apply many of the ideas from functional programming.

Unfortunately, much of the literature on functional programming is difficult to understand for people who are new to it. This short book offers a pragmatic, approachable introduction to functional programming. While aimed at the Java developer, the principles are general and will benefit anyone familiar with an object-oriented language.

I assume that you are well versed in object-oriented programming and you can read Java code. You’ll find some exercises at the end of each chapter to help you practice and expand on what you’ve learned.

Because this is a short introduction and because it is difficult to represent some functional concepts in Java, there will be several topics that I won’t discuss in the text, although I have added glossary entries, for completeness. These topics include currying, partial application, and comprehensions. I’ll briefly discuss several other topics, such as combinators, laziness, and monads, to give you a taste of their importance. However, fully understanding these topics isn’t necessary when you’re new to functional programming.

I hope you find functional programming as seductive as I did. Let me know how it goes!

You can learn more at the book’s catalog page (http://oreilly.com/catalog/9781449311032/).

Conventions Used in This Book

The following typographical conventions are used in this book:


Indicates new terms, URLs, email addresses, filenames, and file extensions. Many italicized terms are defined in the Glossary.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.


This icon signifies a tip, suggestion, or general note.


This icon indicates a warning or caution.

Using the Code Examples

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Functional Programming for Java Developers, by Dean Wampler (O’Reilly). Copyright 2011 Dean Wampler, 978-1-449-31103-2.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at permissions@oreilly.com.

Some of the code examples were adapted from examples provided with the Akka distribution, copyright © 2009-2011 Scalable Solutions AB. The Akka code base is covered by the Apache 2 License.

Getting the Code Examples

You can download the code examples from http://examples.oreilly.com/9781449311032/. Unzip the files to a convenient location. See the README file in the distribution for instructions on building and using the examples.

Note that some of the files won’t actually compile, because they introduce speculative concepts that aren’t supported by current compilers or libraries. Those files end with the extension .javax. (The build process skips them.)

Safari® Books Online


Safari Books Online is an on-demand digital library that lets you easily search over 7,500 technology and creative reference books and videos to find the answers you need quickly.

With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features.

O’Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O’Reilly and other publishers, sign up for free at http://my.safaribooksonline.com.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:


To comment or ask technical questions about this book, send email to:

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia


I want to think my editor at O’Reilly, Mike Loukides, who suggested that I write this book. Brendan McNichols and Bobby Norton provided helpful feedback on drafts of the book. Debasish Ghosh provided valuable comments on the Liskov Substitution Principle and suggested the Olin Shivers quotes on the meaning of foldLeft and foldRight [Shivers]. Daniel Spiewak provided invaluable feedback that helped clarify many of the concepts in the book, such as Monads.

I have learned a lot about functional programming from fellow developers around the world, many of whom are fellow Scala enthusiasts. Martin Odersky, Jonas Bonér, Debasish Ghosh, James Iry, Daniel Spiewak, Simon Peyton Jones, Rich Hickey, Conal Elliot, David Pollak, Paul Snively, and others have illuminated dark corners with their writing, speaking, personal conversations, and code! Finally, my fellow members of the Chicago Area Scala Enthusiasts (CASE) group have also been a source of valuable feedback and inspiration over the last several years.

Of course, any errors and omissions are mine alone.

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