Functional Programming in Java, Second Edition [2 ed.]
9781680509793
Imagine writing Java code that reads like the problem statement, code that's highly expressive, concise, easy to re
142
18
2MB
English
Pages 388
Year 2023
Report DMCA / Copyright
DOWNLOAD EPUB FILE
Table of contents :
Functional Programming in Java, S econd Edition
About the Pragmatic Bookshelf
Table of Contents
Foreword to the First Edition
Praise for Functional Programming in Java, Second Editio n
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
Chapter 1: Hello, Lambda Expressions!
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
Chapter 2: 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
Chapter 3: 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
Chapter 4: 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
Chapter 5: Designing with Lambda Expressions
Separating Concerns Using Lambda Express ions
Delegating Using Lambda Expressions
Decorating Using Lambda Expressions
A Peek into the default Methods
Creating Fluent Interfaces Using L ambda Expressions
Wrapping Up
Chapter 6: Working with Resources
Cleaning Up Resources
Using the Execute Around Method Pattern to Cl ean Up Resources
Managing Locks
Creating Concise Exception Tests
Wrapping Up
Chapter 7: Being Lazy
Delayed Initialization
Lazy Evaluations
Leveraging the Laziness of Str eams
Creating Infinite, Lazy Collec tions
Wrapping Up
Chapter 8: Optimizing Recursions
Using Tail-Call Optimization
Speeding Up with Memoization
Wrapping Up
Chapter 9: Composing Functions with Lambda Expressions
Using Function Composition
Using MapReduce
Taking a Leap to Parallelize
Wrapping Up
Chapter 10: 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
Chapter 11: 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
Chapter 12: 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
Chapter 13: Bringing It All Together
Essential Practices to Succeed with the Functional Style
Performance Concerns
Adopting the Functional Style
Appendix 1: Starter Set of Functional Interfaces
Consumer < T >
Supplier < T >
Predicate < T >
Function < T, R >
Appendix 2: Syntax Overview
Defining a Functional Interface
Creating No-Parameter Lambda Expressions
Creating a Single-Parameter Lambda Expression
Inferring a Lambda Expression’s Parameter Typ e
Dropping Parentheses for a Single-Parameter I nferred 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 E xpression
Lexical Scoping in Closures
Passing a Method Reference of an Instance Met hod
Passing a Method Reference to a static Method
Passing a Method Reference to a Method on Ano ther Instance
Passing a Reference of a Method That Takes Pa rameters
Using a Constructor Reference
Function Composition
Appendix 3: Web Resources
Bibliography
You May Be Interested In…