Java Functional Interface | Lambda Expression in Java

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

The last session was all about Java Iterator and this is a new turn of our learning called Java Functional Interface. In this tutorial, we are going to learn about Java functional interfaces and @FunctionalInterface Annotation in Java. In @FunctionalInterface Annotation, we will discuss Java.util.function package and its examples. Along with this, we will discuss Lambda Expression in Java programming language.

So, let’s start with Java Functional Interface.

Java Functional Interface | Lambda Expression in Java

Java Functional Interface | Lambda Expression in Java

Java Functional Interface (Pre-requisites)

  • Java functional interface is an interface that has one abstract method and it can display only one functionality.
  • Lambda expression in Java is used to represent a functional interface or an interface since Java 8.

Do you know What is Interface in Java? 

Java Functional Interface Example 1-

class Test
{
public static void main(String args[])
{
new Thread(new Runnable()
{
@Override
public void run()
{
System.out.println("New thread created");
}
}).start();
}
}// we had to create anonymous inner classes before Java 8

Java Functional Interface Example 2-

// use of Lambda expressions
class Test
{
public static void main(String args[])
{
// lambda expression
new Thread(()->
{System.out.println("New thread created");}).start();
}
}

Let’s Define Serialization and Deserialization in Java in Detail

@FunctionalInterface Annotation in Java

@FunctionalInterface annotation is employed to make sure that the functional interface can’t have over one abstract method. Just in case over one abstract method is present, the compiler flags an ‘Unexpected @FunctionalInterface annotation’ message. However, it’s not necessary to use this annotation.

Example –

@FunctionalInterface
interface Square
{
int calculate(int x);
}
class Test
{
public static void main(String args[])
{
int a = 5;
Square s = (int x)->x*x;
int ans = s.calculate(a);
System.out.println(ans);
}
}

a. java.util.function Package

The java.util.function package in Java 8 contains many built-in functional interfaces in Java like-

  • Predicate: An abstract method test which gives a Boolean value as a result of the specified argument.
public Predicate
{
public boolean test(T t);
}
  • Binary operator: An abstract method that returns two values and returns the same type.

Read about Java Abstract Data Type in Data Structure – ADT

public interface BinaryOperator
{
public T apply(T x, T y);
}
  • Function: An abstract datatype that takes the type T and returns type R.
public interface Function
{
public R apply(T t);
}

Example –

import java.util.*;
import java.util.function.Predicate;
class Test
{
public static void main(String args[])
{
List<String> names =
Arrays.asList("Falir","FalirQuiz","g1","QA","Flair22");
Predicate<String> p = (s)->s.startsWith("G");
for (String st:names)
{
if (p.test(st))
System.out.println(st);
}
}
}

Lambda Expression in Java 8

Java Lambda expressions primarily express instances of purposeful interfaces (An interface with single abstract methodology is named Java functional interface. An example is java.lang.Runnable). The Lambda expressions in Java, implement the sole abstract function and so implement functional interfaces.

Let’s discuss Java Regular Expression (Java Regex) with Examples

Lambda expressions are a side in Java eight and supply below functionalities.

Enable to treat functionality as a method argument, or code as data.

A function that may be created while not belonging to any class.

Java lambda expression will be passed around as if it had been an object and executed on demand.

interface FuncInterface
{
void abstractFun(int x);
default void normalFun()
{
System.out.println("Hello");
}
}
class Test
{
public static void main(String args[])
{
FuncInterface fobj = (int x)->System.out.println(2*x);
fobj.abstractFun(5);
}
}

Lambda Expression in Java 8

Java Lambda Expression Syntax –

lambda operator -> body

Where lambda operator can be:

  • Zero Parameter
() -> System.out.println("Zero parameter lambda");
  • One Parameter
(p) -> System.out.println("One parameter: " + p);
  • Multiple Parameters
(p1, p2) -> System.out.println("Multiple parameters: " + p1 + ", " + p2);

Do you know What is Multithreading in Java 

Example –

import java.util.ArrayList;
class Test
{
public static void main(String args[])
{
ArrayList<Integer> arrL = new ArrayList<Integer>();
arrL.add(1);
arrL.add(2);
arrL.add(3);
arrL.add(4);
arrL.forEach(n -> System.out.println(n));
arrL.forEach(n -> { if (n%2 == 0) System.out.println(n); });
}
}

Output-
1
2
3
4
2
4
A Java program to demonstrate the working of Java lambda expression with two arguments.

public class Test
{
interface FuncInter1
{
int operation(int a, int b);
}
interface FuncInter2
{
void sayMessage(String message);
}
private int operate(int a, int b, FuncInter1 fobj)
{
return fobj.operation(a, b);
}
public static void main(String args[])
{
FuncInter1 add = (int x, int y) -> x + y;
FuncInter1 multiply = (int x, int y) -> x * y;
Test tobj = new Test();
System.out.println("Addition is " +
tobj.operate(6, 3, add));
System.out.println("Multiplication is " +
tobj.operate(6, 3, multiply));
FuncInter2 fobj = message ->System.out.println("Hello "
+ message);
fobj.sayMessage(“flair");
}
}

Output-
Addition is 9
Multiplication is 18
Hello flair

Let’s explore Three Types of Comments in Java

So, this was all about Java Functional Interface Tutorial. Hope you like our explanation of Java Lambda Expression.

Conclusion

Hence, in this Java Functional Interface tutorial, we learned about what is functional interfaces in java with example. In addition, we discussed Java Lambda expressions and @FunctionalInterface Annotation in Java, in which we cover java.util.function Package. Furthermore, if you have any query regarding Java Functional Interface, feel free to ask in the comment section.

Related Topic- Checked vs Unchecked Exceptions in Java

For reference

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

courses

DataFlair Team

DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.

Leave a Reply

Your email address will not be published. Required fields are marked *