Functional Programming in Java, 2nd Edition

Book description

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.

Publisher resources

View/Submit Errata

Table of contents

  1.  Foreword to the First Edition
  2.  Acknowledgments
  3.  Preface
    1. Who’s This Book For
    2. What’s in This Book
    3. Java Version Used in This Book
    4. How to Read the Code Examples
    5. Online Resources
  4. 1. Hello, Lambda Expressions!
    1. Change the Way You Think
    2. The Big Gains of Functional-Style Code
    3. Why Code in the Functional Style?
    4. Evolution, Not Revolution
    5. A Little Sugar to Sweeten
    6. Wrapping Up
  5. 2. Using Collections
    1. Iterating through a List
    2. Transforming a List
    3. Finding Elements
    4. Skipping Values
    5. Terminating Iterations
    6. Reusing Lambda Expressions
    7. Using Lexical Scoping and Closures
    8. Picking an Element
    9. Reducing a Collection to a Single Value
    10. Joining Elements
    11. Wrapping Up
  6. 3. Strings, Comparators, and Filters
    1. Iterating a String
    2. Implementing the Comparator Interface
    3. Multiple and Fluent Comparisons
    4. Using the collect Method and the Collectors Class
    5. Listing All Files in a Directory
    6. Listing Select Files in a Directory
    7. Listing Immediate Subdirectories Using flatMap
    8. Watching a File Change
    9. Wrapping Up
  7. 4. Transforming Data
    1. Computing Statistics
    2. When to Use map vs. flatMap
    3. Checking for Criteria
    4. Partitioning a Collection
    5. Counting Occurrences
    6. Summing Values
    7. Using flatMapping and filtering
    8. Teeing Operations
    9. Wrapping Up
  8. 5. Designing with Lambda Expressions
    1. Separating Concerns Using Lambda Expressions
    2. Delegating Using Lambda Expressions
    3. Decorating Using Lambda Expressions
    4. A Peek into the default Methods
    5. Creating Fluent Interfaces Using Lambda Expressions
    6. Wrapping Up
  9. 6. Working with Resources
    1. Cleaning Up Resources
    2. Using the Execute Around Method Pattern to Clean Up Resources
    3. Managing Locks
    4. Creating Concise Exception Tests
    5. Wrapping Up
  10. 7. Being Lazy
    1. Delayed Initialization
    2. Lazy Evaluations
    3. Leveraging the Laziness of Streams
    4. Creating Infinite, Lazy Collections
    5. Wrapping Up
  11. 8. Optimizing Recursions
    1. Using Tail-Call Optimization
    2. Speeding Up with Memoization
    3. Wrapping Up
  12. 9. Composing Functions with Lambda Expressions
    1. Using Function Composition
    2. Using MapReduce
    3. Taking a Leap to Parallelize
    4. Wrapping Up
  13. 10. Error Handling
    1. Taking a Holistic View of Error Handling
    2. Exception Handling and Imperative Style
    3. Checked Exceptions and Functional Style with Streams
    4. Exception Handling vs. Functional Style
    5. Dealing with It Downstream
    6. Handling Failures in Functional Programming
    7. Wrapping Up
  14. 11. Refactoring to Functional Style
    1. Creating a Safety Net for Refactoring
    2. Refactoring the Traditional for Loop
    3. Refactoring More Complex Loops
    4. Refactoring Unbounded Loops
    5. Refactoring for-each
    6. Refactoring to Rework the Logic
    7. Refactoring File Processing
    8. Refactoring Data Grouping Operations
    9. Refactoring Nested Loops
    10. Real-World Refactoring
    11. Wrapping Up
  15. 12. Functional Programming Idioms
    1. Don’t Write Dense Lambda Expressions
    2. Prefer Method References
    3. Properly Structure the Functional Pipeline
    4. Keep Separate Conditions in Separate Filters
    5. Provide Good Domain-Specific Parameter Names
    6. Use Type Inference for Parameters
    7. Side Effects in Functional Pipelines
    8. Wrapping Up
  16. 13. Bringing It All Together
    1. Essential Practices to Succeed with the Functional Style
    2. Performance Concerns
    3. Adopting the Functional Style
  17. A1. Starter Set of Functional Interfaces
  18. A2. Syntax Overview
  19. A3. Web Resources
  20.  Bibliography

Product information

  • Title: Functional Programming in Java, 2nd Edition
  • Author(s): Venkat Subramaniam
  • Release date: July 2023
  • Publisher(s): Pragmatic Bookshelf
  • ISBN: 9781680509793