Monday 12 March 2018

What is Functional Interface in Java 8 ~ foundjava

Functional Interface in Java 8

Java 8 Functional Interfaces ~ foundjava

In this java 8 tutorial, We are going to learn new feature of java 8 which is functional interface.

There are so many new features are introduced in java 8 e.g default and static method, new date and time APILambda Expressions etc. functional interface is one of them.

Let's start with the definition of functional interface.


What is Functional Interface in Java 8?

Functional interface is a interface which contains only one abstract method. We can call functional interface as Single Abstract Method Interface(SAM).

We can declare any number of default and static methods in java functional interface. 

We can also declare methods of Object class in Functional Interfaces.

By using functional interfaces in java 8 we can achieve functional programming. 

There are many predefined Functional Interfaces in java 8 which is provided by java and we can create own functional interface in java.

Functional interface provides target type for lambda expressions and method references.


Predefined Functional Interfaces in Java 8

There are many predefined functional interfaces in java which are available in java.util.function package. Some of them are given below...

1) BiConsumer<T, U>

It represents an operations that accepts two input arguments and returns no result.

2) BiFunction<T, U, R>

It represents a function that accepts two arguments and produce a result.

3) BinaryOperator<T>

It represents an operation upon two operands of the same type, producing a result of the same type as the operands.

4) BiPredicate<T, U>

It represents a predicate(boolean - value function) of two arguments.

5) BooleanSupplier

It represents supplier of boolean-valued result.

6) Consumer<T>

It represents an operations that accepts a single input argument and returns no result.

7) DoubleBinaryOperator

It represents an operations upon two double valued operands and producing double valued results.

8) DoubleFunction<R> 

It represents a function that accepts a double valued arguments and produces result.

9) ToDoubleBiFunction<T, U>

It represents a function that accepts two arguments and produces a double valued result.

10) ToIntBiFunction<T, U>

It represents a function that accepts two arguments and produces an int valued result.

Let's take a look some examples of Functional Interface.


Java 8 Functional Interface Example

This is simple functional interface example in java 8 new feature.

@FunctionalInterface//It is optional
interface Dog
{
void run(String s);//Single abstract method
}

class Test implements Dog
{
public void run(String s)
{
System.out.println(s);
}
public static void main(String args[])
{
Test t =
new Test();
t.run("run fast");
}
}

Output: run fast


Predefined Functional Interface Example


This is simple example of predefined functional interface and here we will use lambda expressions.

import java.util.function.IntBinaryOperator;
class PredefinedExample
{
public static void main(String args[])
{
//using  java 8 lambda expressions
IntBinaryOperator add = (x, y) -> x + y;
System.out.println(add.applyAsInt(9, 91));
}
}

Output: 100


Object Class Method in Functional Interface

We can declare only one abstract method in functional interface. We can also declare Object class method in Functional Interface.

@FunctionalInterface
interface Dog
{
void run(String s);//abstract method
String toString();//method of Object class
boolean equals(Obj o);//method of Object class
}

In this functional interface tutorial of java 8, we have learned what is a functional interface in java with simple examples e.g with lambda expressions and without lambda expressions.

1 comment:

  1. This post is really nice and informative. The explanation given is really comprehensive and informative..
    java training in mumbai
    java course in mumbai

    ReplyDelete