Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

What is Functional Interface in Java 8 ~ foundjava

Functional Interface in Java 8


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

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

2) BiFunction

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

3) BinaryOperator

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

4) BiPredicate

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

5) BooleanSupplier

It represents supplier of boolean-valued result.

6) Consumer

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 

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

9) ToDoubleBiFunction

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

10) ToIntBiFunction

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.


This post first appeared on Found Java, please read the originial post: here

Share the post

What is Functional Interface in Java 8 ~ foundjava

×

Subscribe to Found Java

Get updates delivered right to your inbox!

Thank you for your subscription

×