Jump to ratings and reviews
Rate this book

Learning Java Functional Programming

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

About This BookExplore how you can blend object-oriented and functional programming styles in JavaUse lambda expressions to write flexible and succinct codeA tutorial that strengthens your fundamentals in functional programming techniques to enhance your applicationsWho This Book Is ForIf 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 LearnUse lambda expressions to simplyfy codeUse function composition to achieve code fluencyApply streams to simply implementations and achieve parallelismIncorporate recursion to support an application's functionalityProvide more robust implementations using OptionalsImplement design patterns with less codeRefactor object-oriented code to create a functional solutionUse debugging and testing techniques specific to functional programsIn DetailFunctional 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 approachIn 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.

298 pages, Kindle Edition

Published October 14, 2015

1 person is currently reading
9 people want to read

About the author

Ratings & Reviews

What do you think?
Rate this book

Friends & Following

Create a free account to discover what your friends think of this book!

Community Reviews

5 stars
1 (8%)
4 stars
7 (58%)
3 stars
2 (16%)
2 stars
2 (16%)
1 star
0 (0%)
Displaying 1 - 2 of 2 reviews
Profile Image for Zbyszek Sokolowski.
299 reviews16 followers
September 22, 2017
I have found the book to pretty helpful. It shouldn't be the only book on the subject but using it as filing of others could be very beneficial. It is not rather packed with Functional Programming theory comparing to other books and this is not full reference of Java 8. It covers some interesting topics like , monads, using optional, reusing functional interfaces, using design patterns and so on in a new functional world. A lot of practical knowledge can be found inside compared to other more theoretical books. I enjoyed it.

Some excerpts:

The compose method will execute the function passed to it first, and then uses its result with the function the compose method is executed against. The andThen method will execute the first function and then execute the function passed as an argument to the andThen method.

A stream is processed lazily, which enables the runtime system to optimize how the stream's component operations are executed. In addition, they are used in a fluent manner.

The Optional type has a monadic structure. A monad is basically a structure containing a set of computations represented as a series of steps. These computations are chained together effectively forming a pipeline. However, there is more to monads than this. Monads are a very useful technique and promote more reliable programs than most imperative programming techniques are capable of doing.

The advantage of functional interfaces is that Java is able to automatically use a lambda expression that matches the signature of the abstract method found in a functional interface.

Closure refers to functions that enclose variable external to the function. This permits the function to be passed around and used in different contexts.

We can group these interfaces into five categories: Function: These transform their arguments and return a value Predicate: These are used to perform a test, which returns a Boolean value Consumer: These use their arguments, but do not return a value Supplier: These are not passed data, but do return data Operator: These perform a reduction type operation

There is a findFirst method that truncates the stream when an element that meets a specific condition occurs. If we used a method such as findFirst, then lazy evaluation avoids processing elements that are never used. This is critical for infinite streams. An infinite stream would never terminate if eager evaluation were used. Lazy evaluation makes it possible to have infinite streams.

A mathematical understanding occurs when one can clearly see how the recursive invocation works at a functional level and how local variables are handled. This is an important level of understanding to achieve where one is better able to create and maintain recursive application. A more complete understanding involves being able to explain how the program stack implements recursive programs. This level, which is closely tied to the previous level, will enable the programmer to fully understand how recursion works and assist in debugging the more difficult recursive programming errors.

Head recursion occurs when the recursive call occurs at the beginning of the method. It cannot actually be the first statement because this will lead to unbounded recursion. The first statement is typically a base case.

The Optional instances cannot be sorted using the Arrays class's sort method. If used, it throws a java.lang.ClassCastException. This is because an Optional instance cannot be cast to Comparable. However, an array of Optional values can be sorted manually.

Monads A monad structure can be thought of as a chain of operations wrapped around an object. These operations are executed against an object and return some value. In this sense, monads support function composition. The chaining sequence allows programmers to create pipelines, a sequence of operations, to solve their problems. In addition, monads allow operations against the contained values without having to extract them.

A monad uses two operations: The unit operation: This takes some values and uses a constructor to place it into a monadic container. A monadic container can be thought of as a class in Java The bind operation: This uses a monadic value and a function (which converts its argument into a monadic value) and returns a new monadic value

Monads are containers for values, which allow operations to be performed on these values using chains or pipeline approach. The value is contained within the monad. Operations are performed on the value within the monad without extracting the value first. These operations can be chained together using a fluent style of programming.

In addition, monads support the following rules: Associativity Left identity Right identity
Profile Image for David Rissato Cruz.
25 reviews6 followers
June 22, 2016
It isn't a bad book, it is overall well explained but incomplete.
One thing I miss in this book is a depth explanation on Java Collectors. It gave no attention at all to them, restricting itself to using toList() here and there without any deeper explanation.
I also have found small conceptual flaws in some examples, that can lead to confusion.
In my opinion, this book can be used as a supportive material when you are studying from another sources, but I wouldn't recommend it as your primary source.
Displaying 1 - 2 of 2 reviews

Can't find what you're looking for?

Get help and learn more about the design.