Functional Interfaces in Java 8

What is the functional interface?

Java 8 introduced a new powerful feature called lambda expressions.
A lambda is an anonymous function that instance can be passed or returned from a method.
These interfaces are also called Single Abstract Method interfaces (SAM Interfaces).

functional interfaces can be represented using method reference and constructor references as well.
Annotation for the functional interface is @FunctionalInterface.

Package java.util.function Description Functional interfaces provide target types for lambda expressions and method references.
Each functional interface has a single abstract method, called the functional method for that functional interface,
to which the lambda expression's parameter and return types are matched or adapted.
Functional interfaces can provide a target type in multiple contexts, such as assignment context, method invocation, or cast context:

// Assignment context Predicate p = String::isEmpty; // Method invocation context stream.filter(e -> e.getSize() > 10)... // Cast context stream.map((ToIntFunction) e -> e.getSize())...

The interfaces in this package are general-purpose functional interfaces used by the JDK

and are available to be used by user code as well.
While they do not identify a complete set of function shapes to which lambda expressions might be adapted, they provide enough to cover common requirements.
Other functional interfaces provided for specific purposes, such as FileFilter, are defined in the packages where they are used.

Java Predefined-Functional Interfaces Java provides predefined functional interfaces to deal with functional programming
by using lambda and method references.

You can also define your own custom functional interface.
Following is the list of the functional interface which are placed in java.util.function package.

 Here is the list of functional interfaces. We will see frequently used interfaces.


  
 BiConsumer<T,U>  represents an operation that accepts two input arguments and returns no result.
 Function<T,R>  represents a function that accepts one argument and returns a result.
Predicate<T> represents a predicate (boolean-valued function) of one argument.
BiFunction<T,U,R> represents a function that accepts two arguments and returns a a result.
BinaryOperator<T> represents an operation upon two operands of the same data type. It returns a result of the same type as the operands.
BiPredicate<T, U> represents a predicate (boolean-valued function) of two arguments.
BooleanSupplier It represents a supplier of boolean-valued results.
DoubleBinaryOperator represents an operation upon two double type operands and returns a double type value.
DoubleConsumer represents an operation that accepts a single double type argument and returns no result.
DoubleFunction<R> represents a function that accepts a double type argument and produces a result.
DoublePredicate represents a predicate (boolean-valued function) of one double type argument.
DoubleSupplier represents a supplier of double type results.
DoubleToIntFunction represents a function that accepts a double type argument and produces an int type result.
DoubleToLongFunction  represents a function that accepts a double type argument and produces a long type result.
UnaryOperator<T> represents an operation on a single operand that returnsa a result of the same type as its operand.
ToLongFunction<T> a function that returns a result of long type.
ToLongBiFunction<T,U> a function that accepts two arguments and returns a result of long type.
ToIntFunction<T>  a function that returns an integer.
ToIntBiFunction<T,U> a function that accepts two arguments and returns an integer.
DoubleFunction<T> a function that returns a double type result.
ToDoubleBiFunction<T,U> a function that accepts two arguments and produces a double type result.
Supplier<T> a supplier of results.
ObjLongConsumer an operation that accepts an object and a long argument, it returns no result.
ObjIntConsumer an operation that accepts an object and an integer argument. It does not return result.
ObjDoubleConsumer an operation that accepts an object and a double argument, and returns no result.
LongUnaryOperator an operation on a single long type operand that returns a long type result.
LongToIntFunction a function that accepts a long type argument and returns an integer result.
LongToDoubleFunction a function that accepts a long type argument and returns a result of double type.
LongSupplier a supplier of long type results.
LongPredicate a predicate (boolean-valued function) of one long type argument.
LongFunction a function that accepts a long type argument and returns a result.
LongConsumer an operation that accepts a single long type argument and returns no result.
LongBinaryOperator an operation upon two long type operands and returns a long type result.

1). Interface Predicate

This is a functional interface and can, therefore, be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface Predicate

Represents a predicate (boolean-valued function) of one argument. This is a functional interface whose functional method is test(Object).

@FunctionalInterface public interface Predicate { boolean test(T t); }

package com.javacodestuffs.java8.predicate; import java.util.Arrays; import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; public class Java8PredicateExample { public static void main(String[] args) { List list = Arrays.asList("J", "JAVA", "JAVA8", "JAVA8PREDICATE", "KK", "HELLO","WORLD","JARA","JWAR"); System.out.println(Java8StringProcessor.filter( list, x -> x.startsWith("J"))); // [J, JAVA, JAVA8, JAVA8PREDICATE, JARA, JWAR] System.out.println(Java8StringProcessor.filter( list, x -> x.startsWith("J") && x.length() == 4)); // [JAVA, JARA, JWAR] } } output: [J, JAVA, JAVA8, JAVA8PREDICATE, JARA, JWAR] [JAVA, JARA, JWAR] class Java8StringProcessor { static List filter(List list, Predicate predicate) { return list.stream().filter(predicate::test).collect(Collectors.toList()); } }

2). Suppliers

The Supplier functional interface is yet another Function specialization that does not take any arguments.

@FunctionalInterface public interface Supplier

Represents a supplier of results. There is no requirement that a new or distinct result be returned each time the supplier is invoked. This is a functional interface whose functional method is get().

@FunctionalInterface public interface Supplier { T get(); }

package com.javacodestuffs.java8.supplier; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.function.Supplier; public class Java8SupplierExample { private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); public static void main(String[] args) { Supplier supplier1 = () -> LocalDateTime.now(); LocalDateTime datetime1 = supplier1.get(); System.out.println("current datetime without format : "+datetime1); Supplier supplier2 = () -> dtf.format(LocalDateTime.now()); String datetime2 = supplier2.get(); System.out.println("current datetime with format : "+datetime2); } } output: current datetime without format : 2020-06-27T23:27:15.505 current datetime with format : 2020-06-27 23:27

3). Consumer interface

This is a functional interface and can, therefore, be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface Consumer

Represents an operation that accepts a single input argument and returns no result. The consumer is expected to operate via side-effects. This is a functional interface whose functional method is accept(Object).It takes an argument and returns nothing.

@FunctionalInterface public interface Consumer { void accept(T t); }

In the following example accepts Consumer as an argument, simulates a forEach to print length of the String.

package com.javacodestuffs.java8.supplier;
import java.util.Arrays; import java.util.List; import java.util. function.Consumer; public class Java8ConsumerExample { public static void main(String[] args) { List < String > list = Arrays.asList("J", "JAVA", "JAVA8", "JAVA8Consumer", "KK", "HELLO", "WORLD", "JARA", "WAR"); forEach(list, (String x) - >System.out.println(x.length())); } static < T > void forEach(List < T > list, Consumer < T > consumer) { for (T t: list) { consumer.accept(t); } } } output: 1 4 5 13 2 5 5 4 3


How to call the interface method in java?
If methods are static then you can call them by using the class name provided calling method or method present in implementation class should be static
if methods are non-static then you have to create an object of the implementation class.
you can't call methods directly which are non-static in the interface because the interface can't create an object you can't call abstract methods directly because there is no definition or method body.
How to pass interface as parameter in java?
How to implement generic interface in java?
Why functional interface introduced in java 8?
Answer is very simple, Because to support lamda expression if you want to learn
more about lamda expression you can do some research as java taken this feature fron scala so its widely available. How to implement an interface in java?
Can you instantiate an interface in java?
Why to use interface reference in java?
Can we create an object of the interface in java?
When to use abstract class and interface in java with real-time example?
Can we extend an interface in java?
Can a functional interface have static methods?
Predicate functional interface?
Default method in the functional interface?
what are the java 8 features?

In this article, we have seen the Functional Interfaces in Java 8  with examples.