Java 8 Predicates and Functions

12 July 2014
By Gonçalo Marques
 
java java8
In this article we will cover Java 8 Predicate and Function interfaces.

Introduction

Google Guava library users are already familiar with the concepts that we will cover in this article. Predicate and Function are a couple of useful Functional Interfaces introduced in Java 8 (more information about Functional Interfaces in the following article: Java 8 Lambda expressions example).

Let us see which features each one of these interfaces provide.

Predicate

Predicates represent single argument functions that return a boolean value:

Simple predicate
Predicate<Integer> greaterThanTen = (i) -> i > 10;

// Will print true
greaterThanTen.test(14);

Predicates may also be chained together by the means of and, or and negate. Following next is a simple example but one may write complex evaluation rules by chaining predicates:

Predicate chaining
Predicate<Integer> greaterThanTen = (i) -> i > 10;
Predicate<Integer> lowerThanTwenty = (i) -> i < 20;

// Will print true
greaterThanTen.and(lowerThanTwenty).test(15);

// Will print false
greaterThanTen.and(lowerThanTwenty).negate().test(15)

Predicates may also be passed into functions:

Passing predicates into functions
// Will print "Number 10 was accepted!"
process(10, (i) -> i > 7);

void process(int number, Predicate<Integer> predicate) {
  if (predicate.test(number)) {
    System.out.println("Number " + number + " was accepted!");
  }
}

Another example:

Filtering list elements with a predicate
List<User> users = new ArrayList<>();
users.add(new User("John", "admin"));
users.add(new User("Peter", "member"));
List<User> admins = process(users, (u) -> u.getRole().equals("admin"));

List<User> process(List<User> users, Predicate<User> predicate) {
  List<User> result = new ArrayList<>();
  for (User user : users) {
    if (predicate.test(user)) {
      result.add(user);
    }
  }
  return result;
}

Function

Functions also represent a single argument function but they return a result of an arbitrary type:

Simple function
Function<String, Integer> stringLength = (s) -> s.length();

// Will print 11
stringLength.apply("Hello world");

Functions may also be chained:

Function chaining
Function<String, Integer> stringLength = (s) -> s.length();
Function<Integer, Boolean> greaterThanFive = (i) -> i > 5;

// Will print true
stringLength.andThen(greaterThanFive).apply("Hello world");

Another function chaining example:

Another function chaining example
Function<String, Integer> stringLength = (s) -> s.length();
Function<Integer, Boolean> lowerThanTen = (i) -> i < 10;
Function<String, Boolean> function = stringLength.andThen(lowerThanTen);

// Will print false
function.apply("Hello world");

Reference

Lambda Expressions (The Java(TM) Tutorials, Learning the Java Language, Classes and Objects)

Related Articles

Comments

About the author
Gonçalo Marques is a Software Engineer with several years of experience in software development and architecture definition. During this period his main focus was delivering software solutions in banking, telecommunications and governmental areas. He created the Bytes Lounge website with one ultimate goal: share his knowledge with the software development community. His main area of expertise is Java and open source.

GitHub profile: http://github.com/gonmarques

He is also the author of the WiFi File Browser Android application: