Predicate and Function functional interfaces in Java 8 or later

In this example you will see what are Predicate and Function functional interfaces in Java 8 or later version of Java. Functional interfaces provide target types for lambda expressions and method references. Each functional interface has a single abstract method, called functional method for that functional interface, to which the lambda expression’s parameter and return types are matched.

The Predicate functional interface takes an input and returns either true or false. The Function functional interface takes an input and returns a value.

Prerequisites

At least Java 8

Predicate Functional Interface

A predicate statement will be either true or false depending on its variable’s value. It is a function that returns true or false based on condition matches or does not match.

This is a functional interface whose functional method is test(Object).

@FunctionalInterface
public interface Predicate<T> {
	default Predicate<T> and(Predicate<? super T> other);

	static <T> Predicate<T> isEqual(Object targetRef);

	default Predicate<T> negate();

	default Predicate<T> or(Predicate<? super T> other);

	boolean	test(T t);
}

Example on Predicate using lambda expression:

Predicate<String> sp = s -> s.length() > 3;

System.out.println(sp.test("Soumitra"));
System.out.println(sp.test("Hi"));

The output of the above code snippets will be:

true
false

Predicate is also used to filter collections. For example,

List<String> names = Arrays.asList("Soumitra", "Liton", "Arup", "Debabrata", "Sumit");
List<String> filtered = names.stream().filter(s -> s.length() > 4).collect(Collectors.toList());
System.out.println(filtered);

The output from the above code snippets will be:

[Soumitra, Liton, Debabrata, Sumit]

Example on Predicate using method reference can be given as follows:

Predicate<String> str = PredicateExample::length;
System.out.println(str.test("Soumitra"));
System.out.println(str.test("Arup"));

Where length() method is given below:

private static boolean length(String str) {
	return str.length() > 4;
}

The output for the above code snippets will be:

true
false

Function Functional Interface

The Function functional interface takes an argument and returns a value. One of the uses of Function functional interface is to convert or transform from one object to another object. You can pass a lambda expression wherever a function is expected. The type of input parameter and type of returned output may be the same or different.

The Function functional interface has the following signature:

@FunctionalInterface
public interface Function<T,R> {
	default <V> Function<T,V> andThen(Function<? super R,? extends V> after);
	R apply(T t);
	default <V> Function<V,R> compose(Function<? super V,? extends T> before);
	static <T> Function<T,T> identity();
}

The important method in the above signature is apply(T t). It represents a function that accepts one argument and produces a result.

The method R apply(T t) applies this function to the given argument, where t is the parameter that takes the function argument and returns the function result.

Example on Function using lambda expression can be given as follows:

Function<Integer, Integer> fint = i -> i * 4;
System.out.println(fint.apply(5));

Function<String, Integer> fstr = s -> s.length();
System.out.println(fstr.apply("Soumitra"));

The output from the above code snippets will be:

20
8

Using method reference on Function can be given as:

Map<String, Integer> listToMap = convertListToMap(names, FunctionExample::length);
System.out.println(listToMap);

Where convertListToMap() and length() methods are given below:

public static <T, R> Map<T, R> convertListToMap(List<T> list, Function<T, R> func) {

	Map<T, R> result = new HashMap<>();

	for (T t : list) {
		result.put(t, func.apply(t));
	}

	return result;

}

private static Integer length(String str) {
	return str.length();
}

The output from List to Map will be for the above code snippets:

{Soumitra=8, Arup=4, Liton=5, Debabrata=9, Sumit=5}

Hope you got an idea on Java functional interfaces – Function and Predicate. The whole source code you can download from the below section.

Source Code

Download

Author: Soumitra

Hi, I am, Soumitra, the owner of roytuts.com and it is my passion for sharing my knowledge and I have been writing blogs on various technologies since 2014. If you like my tutorials, you may also want to like my Facebook Page, follow me on Twitter, Github.

Leave a Reply

Your email address will not be published. Required fields are marked *