0% found this document useful (0 votes)
2 views6 pages

Java Functional Interfaces

A functional interface in Java contains only one abstract method and can have multiple default or static methods. Java 8 introduced lambda expressions and method references to simplify the implementation of functional interfaces, which include types like Predicate, Consumer, Supplier, and Function. The @FunctionalInterface annotation is optional but recommended to ensure that the interface adheres to the functional interface contract.

Uploaded by

pargemohan047
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Java Functional Interfaces

A functional interface in Java contains only one abstract method and can have multiple default or static methods. Java 8 introduced lambda expressions and method references to simplify the implementation of functional interfaces, which include types like Predicate, Consumer, Supplier, and Function. The @FunctionalInterface annotation is optional but recommended to ensure that the interface adheres to the functional interface contract.

Uploaded by

pargemohan047
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Functional Interfaces

A functional interface in Java is an interface that contains only one abstract


method.
Functional interfaces can have multiple default or static methods, but only one
abstract method.
From Java 8 onwards, lambda expressions and method references can be
used to represent the instance of a functional interface.
Example: Using a Functional Interface with Lambda Expression
public class MIT {
public static void main(String[] args) {
// Using lambda expression to implement Runnable
new Thread(() -> [Link]("New thread created")).start();
}
}

Output
New thread created
Explanation:
 Above program demonstrates use of lambda expression with the
Runnable functional interface.
 Runnable has one abstract method run(), so it qualifies as a functional
interface.
 Lambda ()-> [Link]("New thread created") defines the run()
method.
 new Thread().start() starts a new thread that executes the lambda body
Note:
A functional interface can also extend another functional interface.
@FunctionalInterface Annotation
@FunctionalInterface annotation is used to ensure that the functional interface
cannot have more than one abstract method. In case more than one abstract
methods are present, the compiler flags an "Unexpected @FunctionalInterface
annotation" message. However, it is not mandatory to use this annotation.
Note:
@FunctionalInterface annotation is optional but it is a good practice to use. It
helps catching the error in early stage by making sure that the interface has
only one abstract method.

Example: Defining a Functional Interface with @FunctionalInterface


Annotation
@FunctionalInterface
interface Square {
int calculate(int x);
}
class MIT {
public static void main(String args[]) {
int a = 5;
// lambda expression to define the calculate method
Square s = (int x) -> x * x;
// parameter passed and return type must be same as defined in the
prototype
int ans = [Link](a);
[Link](ans);
}
}

Output
25
Explanation:
 Square is a functional interface with a single method calculate(int x).
 A lambda expression (int x) -> x * x is used to implement the calculate
method.
 Lambda takes x as input and returns x * x.

Java 8 has defined a lot of functional interfaces to be used extensively in lambda


expressions. Following is the list of functional interfaces defined in
[Link] package.

Types of Functional Interfaces in Java


There are primarily four types of functional interfaces in Java.
1. Predicate
2. Consumer
3. Supplier
4. Function
Predicate Functional Interface
A predicate functional interface is the one whose method accepts one argument
and will return either true or false. A predicate functional interface is mainly
used in comparison to sort elements or to filter a value based on certain
condition(s) applied on the input passed.
Usage
Predicate predicate = (value) -> value != 0;
// or
Predicate predicate = (value) -> test(value);
In above snippet predicate function is to return either true/false based on the
value passed.
Example
In this example, were using a predicate functional interface to filter odd
numbers from a list of integers with the help of a lambda expression.
import [Link];
import [Link];
import [Link];

public class Tester {


public static void main(String args[]) {
List<Integer> numbers = [Link](1,2,3,4,5,6,7,8);

Predicate<Integer> isEvenNumber = n -> n %2 == 0;


numbers = [Link]().filter(isEvenNumber).toList();
[Link](numbers);
}
}
This will produce the following result −
[2, 4, 6, 8]
Consumer Functional Interface
A consumer functional interface is one whose method accepts one argument and
will not return anything. A consumer functional interface is mainly used for
side-effect operations. For example, to print an element, to add a salutation, etc..
Usage
Consumer consumer = (value) -> [Link](value);
// Or
Consumer consumer1 = [Link]::println;
// Or
Consumer consumer2 = (value) -> accept(value);
Example
In this example, were using consumer functional interface to print all numbers
from a list of integers with the help of a lambda expression and a method
reference.
import [Link];

Consumer<String> printMessage = msg -> [Link](msg);


[Link]("Welcome to Java 8!");

Supplier Functional Interface


The supplier functional interface is the one whose method does not have any
arguments to pass and will return a value. A supplier functional interface is
mainly used to generate values lazily. For example, to get a random number, to
generate a sequence of numbers, etc.
Usage
Supplier supplier = () -> [Link]() * 10;
// or
Supplier supplier1 = () -> get();
Example
import [Link];

Supplier<String> getGreeting = () -> "Hello, World!";


String greeting = [Link]();
[Link](greeting);

Function Functional Interface


Function functional interface is one whose method accepts one argument and
will return a value.
A function functional interface is mainly used to get the processed value. For
example, to get the square of an element, to trim string values, etc.
Usage
Function function = (value) -> [Link]() * 10;
// or
Function function1 = (value) -> apply(value);
Example
import [Link];

Function<String, Integer> stringToLength = str -> [Link]();


int length = [Link]("Hello");
[Link]("The length of 'Hello' is: " + length);

You might also like