Skip to main content
main-content

Über dieses Buch

Reduce development time by organizing your programs as chains of functional interfaces and see that the advantages of using functional interfaces include the flexibility and power of inlined functional chains and reuse of functional methods utilized throughout the Java API. You’ll see how complex logical expressions can be reduced to chains of predicates and how chains of comparators can be used to sort data by several criteria in order. Other examples include streams that utilize functional interfaces to filter, sort, transform, and perform calculations on data; CompletableFutures that use functional interfaces to create cascading and parallel execution threads; and JavaFX programs that use functional interfaces to monitor the data backed by their graphical components.
Each chapter contains a complete programming project: the Discount Dave project shows you how to qualify car customers by organizing questions as a list of predicates; the Real Estate Broker project shows you how to use chains of comparators to filter and sort homes according to customer priorities; the Dave's Part Inventory project shows you how to query and write reports from an inventory database using stream operations; and the Sentence Builder project shows you how to correct a sentence by implementing each grammar rule as a separate link in a future chain.
Functional Interfaces in Java will help you quickly develop powerful and reliable programs that utilize functional interfaces to implement logic and calculations.

What You Will LearnUse the functional interfaces in the java.util.function package to perform conditional logic, transform and generate data, and perform calculations
Filter and sort data by several criteria using comparators
Process collections and filter, sort, transform, and reduce stream elements with functional interfaces
Write cascading and parallel execution threads

Who This Book Is For
Computer science student or a professional Java programmer. This work is a rigorous discussion of the application of functional interfaces, so prerequisites for this text include basic Java programming and object-oriented Java programming.

Inhaltsverzeichnis

Frontmatter

Chapter 1. Functional Interfaces

Abstract
A functional interface is an interface with a single abstract method, called its functional method. Functional interfaces are ideal for defining a single problem or operation. In Java 8, the API was enhanced to utilize functional interfaces. Many of the functional interfaces can contain static and default methods, making them extendable by the user.
Ralph Lecessi

Chapter 2. Lambda Expressions

Abstract
A lambda expression is used to represent a statement. They were introduced in Java 8 to support functional programming. If your applications require many implementations of functional interfaces, you would have to create many named classes or inline many anonymous class implementations. This would make your code excessively large and difficult to read. Fortunately, Lambda expressions can be used to represent functional interfaces using a very succinct syntax.
Ralph Lecessi

Chapter 3. Predicates

Abstract
In Java 8, the java.util.function package was added to the Java API. It contains many functional interfaces designed to assist with functional programming. Predicate is a functional interface whose functional method, called test, evaluates a condition on an input variable of a generic type. Since the Predicate interface’s default methods return new Predicate objects, chains of predicates can be used to create complex logical expressions.
Ralph Lecessi

Chapter 4. Functions

Abstract
Function is a functional interface with two type parameters T and R. Its functional method, called apply, takes an argument of type T and returns an object of type R. Functions are ideal for converting an object of type T to one of type R. The Function interface has default methods that return new functions, supporting chains of functions that can perform many conversion in series.
Ralph Lecessi

Chapter 5. Operators

Abstract
When a Function object’s input type is the same as its output type, an Operator interface can be used in place of a Function. Operator interfaces define a single type parameter. UnaryOperator is a functional interface with one type parameter T that inherits Function<T, T>. BinaryOperator is a functional interface with one type parameter T that inherits BiFunction<T, T, T>.
Ralph Lecessi

Chapter 6. Consumers

Abstract
Consumer is a functional interface that is used to process data. A Consumer object is specified with type parameter T. Its functional method, called accept, takes an argument of type T and has return type void. Long chains of consumers can be composed to perform an operation that does not require a result to be returned. As with other functional interfaces, the composed consumer of the first two operations becomes the first operand of the second composed consumer, and so on.
Ralph Lecessi

Chapter 7. Suppliers

Abstract
Supplier is a functional interface that is used to generate data. A Supplier object is specified with type para meter T. Its functional method, called get, takes no arguments and returns an object of type T. Using suppliers to wrap each user prompt can help to simplify the logic of a program.
Ralph Lecessi

Chapter 8. Use in Traversing Objects

Abstract
The traversal of data structures has been simplified through the use of consumers which can be used to replace the while loops associated with iterators. In Java 8, the default forEachRemaining method which accepts a consumer was added to the Iterator interface. The PrimitiveIterator interface can be used to traverse a Java array of certain primitive types. In Java 8, the default forEach method, which accepts a consumer, was added to the Iterable interface.
Ralph Lecessi

Chapter 9. Use in Collections

Abstract
Functional interfaces can be used to perform several important operations on collections. They can be used to remove elements from a collection, populate an array, replace the elements of a list or map, perform computations on arrays, maps, and sets, and merge maps.
Ralph Lecessi

Chapter 10. Use in Comparing Objects

Abstract
Functional interfaces have changed the way data is compared in Java. This is due to the enhancements to the Comparator interface, and the addition of several methods that utilize it in the Java API. Comparator is a functional interface that is used to compare two objects. Its functional method, called compare, takes two arguments of type T and returns an integer. Chains of comparators can be built using the Comparator interface’s default thenComparing methods.
Ralph Lecessi

Chapter 11. Use in Optionals

Abstract
It is useful to wrap an object inside an Optional to avoid checking for nullness. Optionals can also be used inside method chains to simplify the logic of a program. The Optional class wraps an object of type parameter T. The Optional class provides methods that can be used to create chains of Optionals. An Optional chain can be used to track consumption of a resource.
Ralph Lecessi

Chapter 12. Use in Streams

Abstract
The Stream interface provides chainable operations that can be performed on a series of values. An object that implements the Stream interface is like an Optional that can contain several values instead of just one. Since many of the methods in the Stream interface return streams, very powerful chains of streams can be created. The Collector interface is used to collect Stream Elements into a mutable reduction. The Collectors class contains static methods which create Collector objects that solve various problems.
Ralph Lecessi

Chapter 13. Use in Multithreaded Programs

Abstract
Runnable and Callable are interfaces whose implementations contain computations meant to be performed in a thread separate from the calling thread. The CompletionStage interface models computations that can be performed as part of a future chain. CompletableFuture is a class that implements CompletionStage, and also implements the Future interface. CompletionStage’s thenApply method accepts a function that operates on the result of the current stage. The method executes after the current stage completes, and returns a new CompletionStage that can be used in a future chain.
Ralph Lecessi

Chapter 14. Use in Atomic Calculations

Abstract
The classes in the java.util.concurrent.atomic package support atomic operations on variables such that a single read or write operation cannot be interrupted by another thread. The AtomicInteger class protects an underlying int value by providing methods that perform atomic operations on the value. Atomic values can be accumulated, updated, and compared. Atomic Longs, Booleans, Arrays, and References are provided.
Ralph Lecessi

Chapter 15. Use in JavaFX Applications

Abstract
JavaFX provides the EventHandler functional interface to handle events. It provides the Builder functional interface which is useful for generating UI components. Factories that generate JavaFX builders can be written using the BuilderFactory functional interface. It is possible to create observable collections in JavaFX. Then listeners can be attached which are invoked when the collections change. JavaFX provides functional interface InvalidationListener that is invoked when an Observable object becomes invalid.
Ralph Lecessi

Backmatter

Weitere Informationen

Premium Partner

    Bildnachweise