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:
- B3.0 2023/02/06
- B2.0 2022/11/16
- B1.0 2022/08/30
Note: Contents and extracts of beta books will change as the book is developed.
- Foreword to the First Edition
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 Candidates
- 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
- 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 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
- 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
- Avoid Side-Effects in Functional Pipeline
- 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
- Syntax Overview
- 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.