Functional Programming in Java, Second Edition
Harness the Power of Streams and Lambda Expressions
by Venkat Subramaniam
Imagine writing Java code that reads like the problem statement, code
that’s highly expressive, concise, easy to read and modify, and has
reduced complexity. With the functional programming capabilities in
Java, that’s not a fantasy. This book will guide you from the familiar
imperative style through the practical aspects of functional
programming, using plenty of examples. Apply the techniques you learn to
turn highly complex imperative code into elegant and easy-to-understand
functional-style code. Updated to the latest version of Java, this
edition has four new chapters on error handling, refactoring to
functional style, transforming data, and idioms of functional
programming.
Don’t struggle with the limitations of the imperative style; instead
learn to combine object-oriented programming with the functional style
to reduce the accidental complexity. Harness the functional programming
capabilities of Java to create applications where the program reveals
its intentions and your team can quickly understand and modify code to
align with changing business requirements. Unlock the power of lambda
expressions and the Streams API to turn the oft-written spaghetti code
into highly concise, expressive, elegant, and maintainable code. See how
Streams make the arduous task of parallelizing code as easy as flipping
a switch when superior speed is necessary.
Apply design patterns built around lambda expressions, safely manage
resource allocations, use memoization, and learn to transform data into
different forms, all while honoring immutability, and providing thread
safety to leverage lazy evaluation for efficiency and parallel execution
for performance. Move beyond the basics, explore the idioms for writing
functional programs. Learn to think functionally by refactoring legacy
code into the functional style. And, if your code runs aground due to
failures, learn to properly handle errors the functional way.
Don’t drown in theory; instead learn the practical functional
programming techniques to create superior Java code.
What You Need
Java version 8 or newer.
Resources
Releases:
- P1.0 2023/08/09
- B4.0 2023/04/07
- B3.0 2023/02/06
- B2.0 2022/11/16
- Foreword to the First Edition
- Acknowledgments
Preface
- Who’s This Book For
- What’s in This Book
- Java Version Used in This Book
- How to Read the Code Examples
- Online Resources
- Hello, Lambda Expressions!
excerpt
- Change the Way You Think
- The Big Gains of Functional-Style Code
- Why Code in the Functional Style?
- Evolution, Not Revolution
- A Little Sugar to Sweeten
- Wrapping Up
- Using Collections
- Iterating through a List
- Transforming a List
- Finding Elements
- Skipping Values
- Terminating Iterations
- Reusing Lambda Expressions
- Using Lexical Scoping and Closures
- Picking an Element
- Reducing a Collection to a Single Value
- Joining Elements
- Wrapping Up
- Strings, Comparators, and Filters
- Iterating a String
- Implementing the Comparator Interface
- Multiple and Fluent Comparisons
- Using the collect Method and the Collectors Class
- Listing All Files in a Directory
- Listing Select Files in a Directory
- Listing Immediate Subdirectories Using flatMap
- Watching a File Change
- Wrapping Up
- Transforming Data
- Computing Statistics
- When to Use map vs. flatMap
- Checking for Criteria
- Partitioning a Collection
- Counting Occurrences
- Summing Values
- Using flatMapping and filtering
- Teeing Operations
- Wrapping Up
- Designing with Lambda Expressions
- Separating Concerns Using Lambda Expressions
- Delegating Using Lambda Expressions
- Decorating Using Lambda Expressions
- A Peek into the default Methods
- Creating Fluent Interfaces Using Lambda Expressions
- Wrapping Up
- Working with Resources
excerpt
- Cleaning Up Resources
- Using the Execute Around Method Pattern to Clean Up Resources
- Managing Locks
- Creating Concise Exception Tests
- Wrapping Up
- Being Lazy
- Delayed Initialization
- Lazy Evaluations
- Leveraging the Laziness of Streams
- Creating Infinite, Lazy Collections
- Wrapping Up
- Optimizing Recursions
- Using Tail-Call Optimization
- Speeding Up with Memoization
- Wrapping Up
- Composing Functions with Lambda Expressions
- Using Function Composition
- Using MapReduce
- Taking a Leap to Parallelize
- Wrapping Up
- Error Handling
- Taking a Holistic View of Error Handling
- Exception Handling and Imperative Style
- Checked Exceptions and Functional Style with Streams
- Exception Handling vs. Functional Style
- Dealing with It Downstream
- Handling Failures in Functional Programming
- Wrapping Up
- Refactoring to Functional Style
- Creating a Safety Net for Refactoring
- Refactoring the Traditional for Loop
- Refactoring More Complex Loops
- Refactoring Unbounded Loops
- Refactoring for-each
- Refactoring to Rework the Logic
- Refactoring File Processing
- Refactoring Data Grouping Operations
- Refactoring Nested Loops
- Real-World Refactoring
- Wrapping Up
- Functional Programming Idioms
- Don’t Write Dense Lambda Expressions
- Prefer Method References
- Properly Structure the Functional Pipeline
- Keep Separate Conditions in Separate Filters
- Provide Good Domain-Specific Parameter Names
- Use Type Inference for Parameters
- Side Effects in Functional Pipelines
- Wrapping Up
- Bringing It All Together
- Essential Practices to Succeed with the Functional Style
- Performance Concerns
- Adopting the Functional Style
- Starter Set of Functional Interfaces
- Consumer
- Supplier
- Predicate
- Function<T, R>
- Syntax Overview
- Defining a Functional Interface
- Creating No-Parameter Lambda Expressions
- Creating a Single-Parameter Lambda Expression
- Inferring a Lambda Expression’s Parameter Type
- Dropping Parentheses for a Single-Parameter Inferred Type
- Creating a Multi-Parameter Lambda Expression
- Calling a Method with Mixed Parameters
- Storing a Lambda Expression
- Creating a Multiline Lambda Expression
- Returning a Lambda Expression
- Returning a Lambda Expression from a Lambda Expression
- Lexical Scoping in Closures
- Passing a Method Reference of an Instance Method
- Passing a Method Reference to a static Method
- Passing a Method Reference to a Method on Another Instance
- Passing a Reference of a Method That Takes Parameters
- Using a Constructor Reference
- Function Composition
- Web Resources
Author
Dr. Venkat Subramaniam is an award-winning author, founder of Agile
Developer, Inc., and an instructional professor at the University of
Houston. He has trained and mentored thousands of software developers in
the US, Canada, Europe, and Asia, and is regularly invited to speak at
international conferences. He’s the (co)author of multiple books,
including the 2007 Jolt Productivity award winning book Practices of
an Agile
Developer.