Chapter 8 - Java for Beginners Course

Built-in Functional Interfaces

When working with lambdas and functional interfaces you’ll find that there are common cases where you’d like to reuse a functional interface definition in several places.

Java provides a set of built-in functional interfaces that is intended to cover some of the common requirements used both in the JDK and that can be used in your code.

The full list can be found in the java.util.function package documentation.

An example of a built-in functional interface

In the example problem that we’ve used over the last sections, we defined a functional interface that we called Filter<E>. In our problem scenario, this interface is responsible of deciding whether an element should be added or not to a result.

public interface Filter<E> {
    boolean filter(E element);
}

If we see this interface in a more general way, this interface receives an element and returns a true/false value, which in functional terms is called a Predicate, that tests if an element complies or not with the definition of our predicate.

You can find the definition of the Predicate<T> interface here, but to simplify, the abstract method it defines is as follows:

public interface Predicate<T> {
	//...

    boolean test(T t);
}

In terms of the requirements we have for our example problem, this is exactly what we want. In that sense, we don’t need to define a Filter<E> interface, we can use the built-in Predicate<T> functional interface that also takes an element and returns a boolean value as a result.

We can then change our code as follows:

In the example code run the JavaListWithLambdaPredicateApp
public class JavaListWithLambdaPredicateApp {

    public static void main(String[] args) {
        // our input list of numbers
        List<Integer> numbers = Arrays.asList(1, 2, 3, 10, 11, 12, 0);

        // call our new `filterNumbers` static method to do the filtering
        List<Integer> filtered = filterNumbers(numbers, element -> element > 5);

        // print out the filtered list
        for (Integer number : filtered) {
            System.out.println(number);
        }
    }

    private static List<Integer> filterNumbers(List<Integer> numbers, Predicate<Integer> filterToUse) {
        List<Integer> filtered = new ArrayList<>();
        // filter the list using the provided `filterToUse`
        for (Integer number : numbers) {
            if (filterToUse.test(number)) {
                filtered.add(number);
            }
        }
        return filtered;
    }
}

Output:

10
11
12
Code in GitHub

Get the code for this tutorial using the links below.

Project Repo
Download code for this step
Main class for this step
Dependencies

This is a list of recommended tutorials or courses that might be useful before starting this one.

Contents
Welcome to the Course!
Course Introduction
Chapter 1 - Building Blocks
Quick introduction to Java Variables Classes And Objects Class Example - Defining a class Object Examples - Creating instances Java Application Example - Running our first app Accessing class members - The dot operator Packages - Organizing the code
Chapter 2 - Primitives and Operators
Primitives Arithmetic Operators Assignment Operator Unary Operators Equality and Relational Operators Conditional Operators
Chapter 3 - Statements and Control Flow
Expressions Statements If-Then Statement If-Then-Else Statement More If Statements Switch Statement While and Do-While Statements For Statement Branching Statements Exception Handling
Chapter 4 - Code Example
Example Project - A Simple Vending Machine Adding money Delivering Items Giving Change
Chapter 5 - Classes and Interfaces
Introduction Access Level Modifiers Class Declaration - Class, Methods and Fields Class Declaration - Constructors Inheritance Basics Inheritance - Constructors Inheritance - Methods and Fields Polymorphism Abstract Classes and Methods Interfaces Static Class Members Class Composition Final Classes and Class Members Generic Classes
Chapter 6 - Base Object Behaviors
Introduction Type Comparison Type Casting Object Equality - The Contract Object Equality - Common Pitfalls Object String Representation Garbage Collection Object Comparison Primitive Wrappers and Autoboxing
Chapter 7 - Data Structures
Introduction Arrays - Declaration and Creation Arrays - Basic Operations Core Collection Interfaces List and ArrayList - Basic Operations ArrayList Internals Introduction to Hash Tables Map and HashMap - Basic Operations Set and HashSet - Basic Operations
Chapter 8 - Anonymous classes and lambdas
Introduction Filtering a List Anonymous Classes Lambdas Built-in Functional Interfaces
Chapter 9 - Streams
Introduction Creating Streams Intermediate Operations Terminal Operations