Learning Java Functional Programming
eBook - ePub

Learning Java Functional Programming

  1. 296 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Learning Java Functional Programming

About this book

Create robust and maintainable Java applications using the functional style of programming

About This Book

  • Explore how you can blend object-oriented and functional programming styles in Java
  • Use lambda expressions to write flexible and succinct code
  • A tutorial that strengthens your fundamentals in functional programming techniques to enhance your applications

Who This Book Is For

If you are a Java developer with object-oriented experience and want to use a functional programming approach in your applications, then this book is for you. All you need to get started is familiarity with basic Java object-oriented programming concepts.

What You Will Learn

  • Use lambda expressions to simplyfy code
  • Use function composition to achieve code fluency
  • Apply streams to simply implementations and achieve parallelism
  • Incorporate recursion to support an application's functionality
  • Provide more robust implementations using Optionals
  • Implement design patterns with less code
  • Refactor object-oriented code to create a functional solution
  • Use debugging and testing techniques specific to functional programs

In Detail

Functional programming is an increasingly popular technology that allows you to simplify many tasks that are often cumbersome and awkward using an object-oriented approach. It is important to understand this approach and know how and when to apply it. Functional programming requires a different mindset, but once mastered it can be very rewarding.

This book simplifies the learning process as a problem is described followed by its implementation using an object-oriented approach and then a solution is provided using appropriate functional programming techniques.

Writing succinct and maintainable code is facilitated by many functional programming techniques including lambda expressions and streams. In this book, you will see numerous examples of how these techniques can be applied starting with an introduction to lambda expressions. Next, you will see how they can replace older approaches and be combined to achieve surprisingly elegant solutions to problems.

This is followed by the investigation of related concepts such as the Optional class and monads, which offer an additional approach to handle problems. Design patterns have been instrumental in solving common problems. You will learn how these are enhanced with functional techniques.

To transition from an object-oriented approach to a functional one, it is useful to have IDE support. IDE tools to refactor, debug, and test functional programs are demonstrated through the chapters. The end of the book brings together many of these functional programming techniques to create a more comprehensive application. You will find this book a very useful resource to learn and apply functional programming techniques in Java.

Style and approach

In this tutorial, each chapter starts with an introduction to the terms and concepts covered in that chapter. It quickly progresses to contrast an object-oriented approach with a functional approach using numerous code examples.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

Learning Java Functional Programming


Table of Contents

Learning Java Functional Programming
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Getting Started with Functional Programming
Aspects of functional programming
Functions
Function composition
Fluent interfaces
Strict versus non-strict evaluation
Persistent data structures
Recursion
Parallelism
Optional and monads
Java 8's support for functional style programming
Lambda expressions
Default methods
Functional interfaces
Method and constructor references
Collections
Summary
2. Putting the Function in Functional Programming
Lambda expressions usage
Functional programming concepts in Java
High-order functions
Returning a function
First-class functions
The pure function
Support repeated execution
Eliminating dependencies between functions
Supporting lazy evaluation
Referential transparency
Closure in Java
Currying
Lambda expressions revisited
Java 8 type inference
Exception handling in lambda expressions
Functional interfaces revisited
Creating a functional interface
Common functional interfaces
Function-type functional interfaces
Predicate-type functional interfaces
Consumer-type functional interfaces
Supplier-type functional interfaces
Operator-type functional interfaces
Summary
3. Function Composition and Fluent Interfaces
Introduction to function composition
Creating composite functions prior to Java 8
Creating composite functions in Java 8
Using the Function interface for function composition
Using the Functional interface to supplement methods
Passing instances of the Functional interface
Fluent interfaces
Fluent interfaces in Java 8
Method chaining and cascading
Contrasting method cascading and fluent interfaces
Creating and using fluent interfaces
Using fluent interfaces to hide older interfaces/classes
Using fluent interfaces with the Properties class
Extending fluent interfaces
Default methods and functions
Static default methods
Default methods in Java 8
Multiple inheritance in Java 8
Summary
4. Streams and the Evaluation of Expressions
The Stream class and its use
Intermediate and terminal methods
Creating streams
Fixed length streams
Infinite streams
Using the iterate method to create an infinite stream
Using the generate method to create an infinite stream
Using the Stream class methods
Filter methods
Using the filter method
Using the skip method
Sorting streams
Mapping methods
Understanding the mapping operation
Implementing the map-reduce paradigm
Using the flatmap method
Lazy and eager evaluation
Stream and concurrent processing
Understanding non-inference
Understanding stateless operations
Understanding side effects
Understanding the ordering
Summary
5. Recursion Techniques in Java 8
Recursive data structures
Types of recursion
Using direct recursion
Head and tail recursion
Understanding recursion
The Node class
Using head recursion
Using tail recursion
Using the head and tail recursion
Creating a recursive solution based on a formula
Converting an iterative loop to a recursive solution
Merging two lists
Understanding the program stack
Recursive lambda expressions
Common problems found in recursive solutions
Absence of a base case
Using static or instance variables
Using the pre- and post-increment operators
Recursion implementation techniques
Using a wrapper method
Using short circuiting
Tail call optimization
Converting to a tail call
When to use recursion
Recursion and humor
Summary
6. Optional and Monads
Using the Optional class
Creating Optional instances
Using the Optional class to support return values
Handling missing values
Using the orElse method to get a substitute value
Using the orElseGet method to use a function to get a substitute value
Using the orElseThrow method to throw an exception
Filter and transforming values
Using the Optional class's filter method
Using the Optional class's map method
Optional solution to the Customer problem
Disadvantages of the Optional class
Monads
Monads in Java 8
Using the of method as the unit function
Using the flatMap method
Using the map method
Using the Optional class with strings
Using monads with the Part class
A formal discussion of monads
Associativity
Left identity
Right identity
Summary
7. Supporting Design Patterns Using Functional Programming
Implementing the execute-around-method pattern
Object-oriented solution to the execute-around-method pattern
Functional solution to the execute-around-method pattern
Using the execute-around-method pattern with a stream
Implementing the factory pattern
Object-oriented solution to the factory pattern
Functional solution to the factory pattern
Implementing the command pattern
Object-oriented solution to the command pattern
Functional solution to the command pattern
Implementing the strategy pattern
Object-oriented solution to strategy pattern
Functional solution to the strategy pattern
Using the Function interface
Implementing the visitor pattern
Object-orient solution to the visitor pattern
Functional solution to the visitor pattern
Implementing the template pattern
Object-oriented solution to the template pattern
Functional solution to the template pattern
Summary
8. Refactoring, Debugging, and Testing
Refactoring functional code
NetBeans support for refactoring
Converting anonymous inner classes to lambda expressions
Refactoring multiple code instances
Support of other refactoring operations
Eclipse support for refactoring
Converting anonymous inner classes to lambda expressions
Refactoring multiple code instances
Support of other refactoring operations
Debugging lambda expressions
Using the printl...

Table of contents

  1. Learning Java Functional Programming

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Learning Java Functional Programming by Richard M Reese in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in Java. We have over one million books available in our catalogue for you to explore.