Predefined Functional Interfaces - Making Java Easy To Learn
Predefined Functional Interfaces - Making Java Easy To Learn
https://javatechonline.com/predefined-functional-interfaces/ 1/8
9/22/22, 2:29 PM Predefined Functional Interfaces | Making Java Easy To Learn
many times we come across re-occurring functionalities to be developed. In that case, we can utilize the predefined functional
interfaces instead of creating our own every time. They will obviously save our development time and minimize chances of mistakes.
Example
For example, suppose we have to write a program to check if a number is a single digit number or not using Lambda expression. Since
it’s a conditional check, we will implement Predicate as below:
Function<T, R>
Function<T, R> is used to perform some operation & returns some result. Unlike Predicate<T> which returns only boolean,
Function<T, R> can return any type of value. Therefore, we can also say that Predicate is a special type of Function which returns only
Boolean values.
interface Function<T,R> {
R apply(T t);
}
Example
For example, suppose we have to write a program to find length of a given String using Lambda expression. Since it’s taking input,
performing some operation & returning result, we will implement Function as below:
Consumer<T>
https://javatechonline.com/predefined-functional-interfaces/ 2/8
9/22/22, 2:29 PM Predefined Functional Interfaces | Making Java Easy To Learn
Consumer<T> is used when we have to provide some input parameter, perform certain operation, but don’t need to return anything.
Moreover, we can use Consumer to consume object and perform certain operation.
interface Consumer<T> {
void accept(T t);
}
Example
For example, suppose we have to write a program to find length of a given String using Lambda expression. Since it’s taking input,
performing some operation & returning result, we will implement Consumer as below:
Supplier<R>
Supplier<R> doesn’t take any input and it always returns some object. However, we use it when we need to get some value based on
some operation like supply Random numbers, supply Random OTPs, supply Random Passwords etc. For example, below code denotes
it :
interface Supplier<R>{
R get();
}
Example
For example, suppose we have to write a program to supply 4 digit random OTPs using Lambda expression. Since it will return values
without taking any input parameter, we will implement Supplier as below:
BiPredicate<T, U>
BiPredicate<T, U> is same as Predicate<T> except that it has two input parameters. For example, below code denotes it:
https://javatechonline.com/predefined-functional-interfaces/ 3/8
9/22/22, 2:29 PM Predefined Functional Interfaces | Making Java Easy To Learn
Example
For example, suppose we have to check the sum of two integers is even or not by using BiPredicate, we will implement BiPredicate<T,
U> as below:
BiFunction<T, U, R>
BiFunction<T, U, R> is same as Function<T, R> except that it has two input parameters. For example, below code denotes it:
Example
For example, suppose we have to find sum of two integers by using BiFunction, we will implement BiFunction<T,U,R> as below:
BiFunction<Integer,Integer,Integer> bf = (i,j)->i+j;
System.out.println(bf.apply(24,4));
BiConsumer<T, U>
BiConsumer<T> is same as Consumer<T> except that it has two input parameters. For example, below code denotes it:
Example
For example, suppose we have to find concatenation of two strings & print result on the console by using BiConsumer, we will
implement BiConsumer<T, U> as below:
Predicate<T>
https://javatechonline.com/predefined-functional-interfaces/ 4/8
9/22/22, 2:29 PM Predefined Functional Interfaces | Making Java Easy To Learn
♦Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.
⇒Returns a predicate that tests if two arguments are equal according to Objects.equals(Object, Object).
⇒Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.
Function<T,R>
⇒Returns a composed function that first applies this function to its input, and then applies the after function to the result.
♦Returns a composed function that first applies the before function to its input, and then applies this function to the result.
Consumer<T>
⇒Returns a composed Consumer that performs, then in sequence, this operation followed by the after operation.
Supplier<R>
BiPredicate<T, U>
⇒Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.
https://javatechonline.com/predefined-functional-interfaces/ 5/8
9/22/22, 2:29 PM Predefined Functional Interfaces | Making Java Easy To Learn
⇒Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.
BiFunction<T, U, R>
♦Returns a composed function that first applies this function to its input, and then applies the after function to the result.
BiConsumer<T, U>
⇒Returns a composed BiConsumer that performs, in sequence, this operation followed by the after operation.
♥ Moreover, If you still want to learn more on Predefined Functional Interface, Kindly visit Oracle Documentation.
Java Interface
July 26, 2022
In "Core Java"
Java 8 Features
July 10, 2021
In "Java 8"
Like
Tagged bifunction java 8 built in functional interfaces built-in functional interfaces in java Built-in functional interfaces in java8 Consumer consumer in java
8 Function function in java 8 Functional Interface Explained in Detail functional interface java functional interfaces in java functional interfaces in java 8 Java
8 Functional Interfaces java function Java Functional Interface java functional interfaces java predefined functional interfaces predefined function predefined
functional interface in java predefined functional interface in java 8 Predefined Functional Interfaces predefined functional interfaces in java predefined functional
interfaces in java 8 predefined interface in java predefined interfaces in java Predicate predicate in java 8 Supplier supplier java what is predicate in java 8
which of the following are built in functional interfaces which one is predefined interface in java
Previous article
Java Interview Questions and Answers
Next article
OOPs Design Principles
Leave a Reply
Comment *
Name *
Email Address *
https://javatechonline.com/predefined-functional-interfaces/ 6/8