Book Image

Learning Java Functional Programming

By : Richard M Reese, Richard M. Reese
Book Image

Learning Java Functional Programming

By: Richard M Reese, Richard M. Reese

Overview of this book

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.
Table of Contents (16 chapters)
Learning Java Functional Programming
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The Stream class and its use


The Stream class provides the primary support for the stream concept in Java. However, there are specialized classes, such as the DoubleStream, IntStream, and LongStream classes, that handle numbers. In addition, the Collection interface supports the creation of streams.

A stream will support either a finite or an infinite sequence of elements. The methods of a stream can be classified in a number of ways such as mapping, filtering, and sorting type methods. We will start with a simple example of a stream, and then follow up with a discussion of how they are created and several of their methods.

Let's assume that we need to process an array of numbers by summing the values of each unique element. For example, in the following array, there are six distinct numbers whose sum is 35:

    int[] numbers = {3,6,8,8,4,6,3,3,5,6,9,4,3,6};

One approach to solve the problem involves:

  1. Finding the distinct numbers in the array.

  2. Summing these numbers.

Finding the distinct numbers...