DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Advanced Brain-Computer Interfaces With Java
  • Simplify Java: Reducing Unnecessary Layers and Interfaces [Video]
  • Java 21 SequenceCollection: Unleash the Power of Ordered Collections
  • Projections/DTOs in Spring Data R2DBC

Trending

  • Distributed Consensus: Paxos vs. Raft and Modern Implementations
  • Implementing Explainable AI in CRM Using Stream Processing
  • AI Agents: A New Era for Integration Professionals
  • Optimizing Serverless Computing with AWS Lambda Layers and CloudFormation
  1. DZone
  2. Coding
  3. Java
  4. Functional Interface Explained in Detail Introduced From Java 8

Functional Interface Explained in Detail Introduced From Java 8

In this article, explore functional interface introduced in Java 8.

By 
Amit Datta user avatar
Amit Datta
DZone Core CORE ·
Updated Mar. 29, 22 · Analysis
Likes (16)
Comment
Save
Tweet
Share
68.2K Views

Join the DZone community and get the full member experience.

Join For Free

Originally published August 2020

Functional interfaces are introduced as part of Java 8. It is implemented using the annotation called @FunctionalInterface. It ensures that the interface should have only one abstract method. The usage of the abstract keyword is optional as the method defined inside the interface is by default abstract. It is important to note that a functional interface can have multiple default methods (it can be said concrete methods which are default), but only one abstract method. The default method has been introduced in interface so that a new method can be appended in the class without affecting the implementing class of the existing interfaces. Prior to Java 8, the implementing class of an interface had to implement all the abstract methods defined in the interface.

The functional interface has been introduced in Java 8 to support the lambda expression. On the other hand, it can be said lambda expression is the instance of a functional interface.

For example:

Java
 




x


 
1
@FunctionalInterface
2

          
3
Interface Area<T,S,R>
4

          
5
{
6

          
7
R calculateArea(T a, S b);
8

          
9
}



It is well known how the above interface is implemented prior to Java 8, but in Java 8, it can be implemented in a smarter way as in the implementation below.

Java
 




xxxxxxxxxx
1


 
1
static Area<Double, Double, Double> area = (a, b) ->(a*b);
2

          
3
System.out.println("The total area is "+ area.calculateArea(1.1,1.1));



The full implementation of the above functional interface is available on GitHub, the link of which will be provided at the end of this tutorial.

In Java 8, there are 4 main functional interfaces are introduced which could be used in different scenarios. These are given below.

  1. Consumer
  2. Predicate
  3. Function
  4. Supplier

Among the above four interfaces, the first three interfaces also have extensions also which are given below.

  • Consumer - BiConsumer
  • Predicate – BiPredicate
  • Function – BiFunction, UnaryOperator, BinaryOperator

1.  Consumer

Let’s discuss Consumer.

  • The Consumer interface accepts one argument, but there is no return value.
  • The name of the function inside this interface is accept.
Java
 




xxxxxxxxxx
1
13


 
1
@FunctionalInterface
2

          
3
public interface Consumer<T> {
4

          
5
void accept(T t);
6

          
7
…
8

          
9
}
10

          
11
Consumer<String> ucConsumer = (s) -> System.out.println(s.toUpperCase());
12

          
13
ucConsumer.accept("consumer");



Output

The above consumer is accepting one argument, and printing it in upper case, but there is no return value.

BiConsumer

The extension of the Consumer, which is BiConsumer, accepts two arguments and returns nothing.

Java
 




xxxxxxxxxx
1
19


 
1
@FunctionalInterface
2

          
3
public interface BiConsumer<T, U> {
4

          
5
void accept(T t, U u);
6

          
7
...
8

          
9
}
10

          
11
BiConsumer<String, String> biConsumer = (x,y) -> {
12

          
13
System.out.println(" x : " + x + " y : " + y );
14

          
15
};
16

          
17
biConsumer.accept("Sun" , "Moon");
18

          
19
Output: x: Sun y: Moon



2.  Predicate

Predicate will accept one argument, do some processing, and then return boolean.

Java
 




xxxxxxxxxx
1
14


 
1
@FunctionalInterface
2

          
3
public interface Predicate<T> {
4

          
5

          
6
boolean test(T t);
7

          
8
...
9

          
10
}
11

          
12
static Predicate<Integer> p = (i) -> {return i%2 ==0;};
13

          
14
System.out.println("Result is p : " + p.test(4));



Output 

True

BiPredicate

Instead of one argument, BiPredicate will accept two arguments and return boolean.

3.  Function

This interface accepts one argument and returns a value after the required processing. It is defined as below. The required processing logic will be executed on the invocation of the apply method.

Java
 




xxxxxxxxxx
1


 
1
@FunctionalInterface
2

          
3
public interface Function<T, R> {
4

          
5
R apply(T t);
6

          
7
…..
8

          
9
}



For example, the above functional interface will be executed while the following code would be executed.

Java
 




xxxxxxxxxx
1


 
1
static Function<String,String> upperCase = (name) -> name.toUpperCase();
2

          
3
System.out.println("Result is : " + upperCase.apply("functional"));



Output

functional

In the above example, the Function is accepting one string and also returns one string.

BiFunction

The BiFunction is similar to Function except it accepts two inputs, whereas Function accepts one argument. The sample code for the BiFunction interface is given below. In the below interface code T and U are the inputs and R is the single output.

Java
 




xxxxxxxxxx
1


 
1
@FunctionalInterface
2

          
3
public interface BiFunction<T, U, R> {
4

          
5
R apply(T t, U u);
6

          
7
……
8

          
9
}



UnaryOperator and BinaryOperator

Another two interfaces are UnaryOperator and BinaryOperator,which extends the Function and BiFunction respectively. The following interface code snippets are given below for these two interfaces.

Java
 




xxxxxxxxxx
1
15


 
1
@FunctionalInterface
2

          
3
public interface UnaryOperator<T> extends Function<T, T> {
4

          
5
…
6

          
7
}
8

          
9
@FunctionalInterface
10

          
11
public interface BinaryOperator<T> extends BiFunction<T,T,T> {
12

          
13
...
14

          
15
}



From the above interfaces, it is easy to understand that the UnaryOperator accepts a single argument and return a single argument, but both the input and output argument should be of same or similar type.

On the other hand, BinaryOperator accepts two arguments and returns one argument similar to BiFunction, but the type of all the input and output argument should be of similar type.

The following examples for UnaryOperator and BinaryOperator interfaces are given respectively.

Java
 




xxxxxxxxxx
1


 
1
UnaryOperator<String> unaryOperator = (s)->s.concat("Operator");
2

          
3
System.out.println(unaryOperator.apply("Unary"));



Output 

UnaryOperator

Java
 




xxxxxxxxxx
1


 
1
BinaryOperator<Integer> binaryOperator = (a,b) -> a+b;
2

          
3
System.out.println(binaryOperator.apply(3,4));



Output: 7

4. Supplier

Supplier functional interface does not accept any input; rather returns a single output. The following interface code is given for understanding.

Java
 




xxxxxxxxxx
1
17


 
1
@FunctionalInterface
2

          
3
public interface Supplier<T> {
4

          
5
/**
6

          
7
* Gets a result.
8

          
9
*
10

          
11
* @return a result
12

          
13
*/
14

          
15
T get();
16

          
17
}



Find the below example:

Java
 




xxxxxxxxxx
1


 
1
public static Supplier<String> helloWorld = () -> {
2

          
3
return "Hello World";
4

          
5
};



Java
 




xxxxxxxxxx
1


 
1
String greeting = helloWorld.get();
2

          
3
System.out.println("The greeting message: "+greeting)



The above is just a simple example but it may be used in a complex business scenario. For all the above scenarios download the sample code from here.

Interface (computing) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Advanced Brain-Computer Interfaces With Java
  • Simplify Java: Reducing Unnecessary Layers and Interfaces [Video]
  • Java 21 SequenceCollection: Unleash the Power of Ordered Collections
  • Projections/DTOs in Spring Data R2DBC

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!