Functional Programming in Java, Second Edition

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

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.

Author(s): Venkat Subramaniam
Edition: 2
Publisher: The Pragmatic Bookshelf, LLC
Year: 2023

Language: English
Pages: 388

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…