top of page
Writer's pictureThe Tech Platform

Top 5 Functional Interface in Java

Updated: Jan 14, 2023

A Java platform is basically a collection of programs that will help you develop and run Java programming applications. It is made up of an execution engine, a compiler, and a set of libraries. It is basically a set of computer software and specifications.


Java can also be used for developing android applications, creating Enterprise Software, creating mobile java applications, creating scientific computing applications, Big Data analytics, programming hardware devices, and server-side technologies like Apache and GlassFish.


Top 5 Functional Interfaces in Java

There are only 5 or 6 actual functions to need, which are as below:


1. Function Interface

The goal of the Function interface is to take a value from the program, perform some type of calculation or operation on that value and then return a new value. Here’s an example of how a Function and lambda expression work together:

Function<Integer, String> verboseLambda = (Integer x)-> 
{ 
    return Integer.toString(x*x); 
};
System.out.println(verboseLambda.apply(5));

The Java Function interface is used in many parts of the java.util.funcion API package

The return keyword on the right-hand side of the lambda expression is the key to the Function interface.


2. Consumer Interface

In contrast to the Function interface, the Consumer interface doesn’t return a value. The Consumer interface is passed a value, performs some type of operation or calculation on that value and then terminates without returning anything. Here is a Consumer interface and lambda expression example:

Consumer<Long> conciseLambda = (Integer t) -> System.out.println(t*t);
conciseLambda.accept(new Long(10));

Again, the difference between the Consumer and Function interface is the fact that the return keyword is absent on the right-hand side of the lambda expression.


3. Supplier Interface

In the previous two examples, an instance of the Integer class was passed to both the Function and the Consumer. The Supplier interface, however, isn’t passed anything. It simply generates a value based solely on its internal logic and then returns that value. Here is a Supplier interface and lambda expression example:

Supplier<Integer> rds = () -> new Random().nextInt(10);


4. Predicate and Streams API

Boolean logic forms the foundation of all computer programs, so it’s no surprise to find out that there is a functional interface dedicates to true or false values. Like the Function and Supplier interface, the Predicate interface is passed a value. The key difference, however, is that when the Predicate interface runs, a true or false value must be returned. Here is a lambda expression and Predicate example:

Predicate<Integer> lambdaPredicate = (Integer x) -> (x % 2 == 0);

The functional Predicate interface gets used extensively by the Java 8 Streams API. Any developer who wants to master functional programming in Java will need to be comfortable with the Predicate interface and will make manipulating collection classes with lambda expressions extremely easy.


5. UnaryOperator

Like the Function interface, the UnaryOperator is passed a value and returns a value. However, the distinction between the UnaryOperator and the Function is that the UnaryOperator must return the exact same type of object that it’s passed. For example, if a “Person” object is passed to the UnaryOperator, a “Person” object must be passed back. This restriction doesn’t apply to the Function interface. Here’s an example of the UnaryOperator in action using a String as the unary type:

UnaryOperator<String> extensionAdder = (String text) -> 
{ 
    return text + ".txt";
} ;


The Tech Platform

0 comments

Comments


bottom of page