A Functional Approach to Java

Book description

Java developers usually tackle the complexity of software development through object-oriented programming (OOP). But not every problem is a good match for OOP. The functional programming (FP) paradigm offers you another approach to solving problems, and Java provides easy-to-grasp FP tools such as lambdas and streams. If you're interested in applying FP concepts to your Java code, this book is for you.

Author Ben Weidig highlights different aspects of functional programming and shows you how to incorporate them in your code without going "fully functional." You'll learn how, when, and why to use FP concepts such as immutability and pure functions to write more concise, reasonable, and future-proof code. Many developers seek to expand their horizons by using OOP and FP together. It's no longer either-or; it's both.

In two parts, this book includes:

  • A Functional Approach: Get a high-level overview of functional programming, including the types already available to Java developers. Then explore different FP concepts and learn how to use them.
  • Real-World Problems, Patterns, and Recipes: Apply what you've learned in part one to the typical real-world problems you might encounter in your daily work.

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. New Hardware Needs a New Way of Thinking
    2. Java can be Functional, too
    3. Why I Wrote This Book
    4. Who Should Read This Book
    5. What You Will Learn
    6. What About Android?
      1. A Functional Approach to Android
    7. Navigating This Book
    8. Conventions Used in This Book
    9. Using Code Examples
    10. O’Reilly Online Learning
    11. How to Contact Us
    12. Acknowledgments
  2. I. Functional Basics
  3. 1. An Introduction to Functional Programming
    1. What Makes A Language Functional?
    2. Functional Programming Concepts
      1. Pure Functions and Referential Transparency
      2. Immutability
      3. Recursion
      4. First-Class and Higher-Order Functions
      5. Functional Composition
      6. Currying
      7. Partial Function Application
      8. Lazy Evaluation
    3. Advantages of Functional Programming
    4. Takeaways
  4. 2. Functional Java
    1. What Are Java Lambdas?
      1. Lambda Syntax
      2. Functional Interfaces
      3. Lambdas and Outside Variables
      4. What about Anonymous Classes?
    2. Lambdas In Action
      1. Creating Lambdas
      2. Calling Lambdas
      3. Method References
    3. Functional Programming Concepts in Java
      1. Pure Functions and Referential Transparency
      2. Immutability
      3. First-Class and Higher-Order
      4. Functional Composition
      5. Lazy Evaluation
    4. Takeaways
  5. 3. Immutability
    1. Mutability and Data Structures in OOP
    2. Immutability (not only) in FP
    3. The State of Java Immutability
      1. Immutable Types and Constructs of the JDK
      2. The final keyword
      3. Records
    4. How to Achieve Immutability
    5. Common Practices
    6. Takeaways
  6. 4. Working With Records
    1. Data Aggregation Types
    2. Records to the Rescue
      1. Record Features
      2. Missing Features
    3. Use-Cases and Common Practices
      1. Record Validation and Data Scrubbing
      2. Increasing Immutablity
      3. Creating Modified Copies
      4. Records as Local Nominal Tuples
      5. Better Optional Data Handling
      6. Serializing Evolving Records
    4. Final Thoughts on Records
    5. Takeaways
  7. 5. Data Processing with Streams
    1. Data Processing with Loops
    2. Streams as Functional Data Pipelines
      1. Stream Features
      2. Spliterator, the Backbone of Streams
    3. Building Stream Pipelines
      1. Creating a Stream
      2. Doing the Work
      3. Terminating the Stream
      4. The Cost of Operations
      5. Modifying Stream Behavior
    4. To Use Streams, or Not?
    5. Takeaways
  8. 6. Working With Streams
    1. Primitive Streams
    2. Iterative Streams
    3. Infinite Streams
      1. Random Numbers
      2. Memory Isn’t Infinite
    4. From Arrays to Streams and Back
      1. Object-Type Arrays
      2. Primitive Arrays
    5. Low-Level Stream Creation
    6. Working with File I/O
      1. Reading Directory Contents
      2. Depth-First Directory Traversal
      3. Searching the Filesystem
      4. Reading Files Line-By-Line
      5. Caveats of File I/O Streams
    7. Dealing with Date and Time
      1. Querying Temporal Types
      2. LocalDate-Range Streams
    8. Measuring Stream Performance with JMH
    9. More about Collectors
      1. Downstream Collectors
      2. Creating Your Own Collector
    10. Final Thoughts on (Sequential) Streams
    11. Takeaways
  9. 7. Parallel Data Processing with Streams
    1. Concurrency Versus Parallelism
    2. Steams as Parallel Functional Pipelines
    3. Parallel Streams in Action
    4. When to Use and When to Avoid Parallel Streams
      1. Choosing the Right Data Source
      2. Number of Elements
      3. Stream Operations
      4. Stream Overhead and Available Resources
      5. Example: War and Peace (revisited)
      6. Example: Random Numbers
    5. Parallel Streams Checklist
    6. Takeaways
  10. 8. Functional Exception Handling
    1. Java Exception Handling in a Nutshell
    2. Checked Exceptions in Lambdas
      1. Safe Method Extraction
      2. Un-Checking Exceptions
      3. Sneaky Throws
    3. A Functional Approach to Exceptions
      1. Not Throwing Exceptions
      2. Errors as Values
      3. The Try/Sucess/Failure Pattern
    4. Final Thoughts on Functional Exception Handling
    5. Takeaways
  11. 9. Lazy Evaluation
    1. Laziness Versus Strictness
    2. How Strict Is Java?
      1. Short-Circuit Evaluation
      2. Control Structures
      3. Lazy Types in the JDK
    3. Lambdas and Higher-Order Functions
      1. An Eager Approach
      2. A Lazier Approach
      3. A Functional Approach
    4. Delayed Executions with Thunks
      1. Creating a Simple Thunk
      2. A Thread-Safe Thunk
    5. Final Thoughts on Laziness
    6. Takeaways
  12. 10. Recursion
    1. What is Recursion?
      1. Head Versus Tail Recursion
      2. Recursion and the Stack
    2. A More Complex Example
      1. Iterative Tree-Traversal
      2. Recursive Tree-Traversal
    3. Recursion-like Streams
    4. Final Thoughts on Recursion
    5. Takeaways

Product information

  • Title: A Functional Approach to Java
  • Author(s): Ben Weidig
  • Release date: June 2023
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098109929