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.

Table of Contents

  1. Preface
    1. New Hardware Needs a New Way of Thinking
    2. Why Java?
    3. Why I Wrote This Book
    4. Who Should Read This Book
    5. What You Will Learn
    6. What About Android?
    7. Android is (not) Java
    8. Desugaring Android Java Code
    9. A Functional Approach to Android
    10. Navigating This Book
    11. Conventions Used in This Book
    12. Using Code Examples
    13. O’Reilly Online Learning
    14. How to Contact Us
    15. Acknowledgments
  2. I. A Functional Approach
  3. 1. An Introduction to Functional Programming
    1. The Origin of Functional Programming
    2. Lambda Abstractions
    3. Application
    4. Reduction
    5. What is Functional Programming?
    6. Expressions
    7. Statements
    8. Functional Programming Concepts
    9. Pure Functions
    10. Referential Transparency
    11. Immutability
    12. Recursion
    13. First-Class and Higher-Order
    14. Functional Composition
    15. Laziness
    16. Advantages of Functional Programming
    17. Academia Versus “The Real World”
    18. Takeaways
  4. 2. Optionals
    1. How to Handle null
    2. Best Practices and Informal Rules
    3. Safe Navigation Operator
    4. Embrace null as valid value
    5. Third-party tools
    6. No null at all and/or specialized types
    7. How Java Handles null with Optionals
    8. Optional<T> Operations
    9. Creating Optionals
    10. Checking for Values
    11. Filtering and Mapping
    12. Getting a (fallback) value
    13. Optional Primitives
    14. Creating an Improved Primitive Wrapper
    15. Using Your Own Optional Types
    16. Optionals and Streams
    17. Optionals as Stream Elements
    18. Terminal Operations
    19. Caveats
    20. Special Considerations for Collections
    21. Alternative Implementations
    22. Is null Really Evil?
    23. Takeaways
  5. 3. Recursion
    1. Mathematical Explanation
    2. Head Versus Tail Recursion
    3. Recursion and the Stack
    4. Streams to the Rescue
    5. A More Complex Example
    6. Recursive Tree-Traversal
    7. Iterative Tree-Traversal
    8. When (Not) To Use Recursion
    9. Takeaways