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 in our learning called Java Functional Interface. In this tutorial, we are going to learn about Java functional interfaces and the @FunctionalInterface Annotation in Java. In the @FunctionalInterface Annotation, we will discuss Java.util.function package and its examples. Along with this, we will discuss Lambda Expression in the Java programming language.
So, let’s start with Java Functional Interface.
Java Functional InterfaceA
- 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.
Characteristics of Functional Interface in Java:
- A functional interface in Java can have various static or default methods.
- The abstract method present in the interface can override the other method available.
- It has an annotation that checks if there is more than one abstract method present in the interface.
Do you know what an interface is 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 8Java 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 does not 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 of @FunctionalInterface Annotation in Java –
@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);
}
}java.util.function Package
The java.util.function package in Java 8 contains many built-in functional interfaces, such as-
Predicate: An abstract method test that 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 of java.util.function Package –
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). However, 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 treating functionality as a method argument, or code as data.
A function that may be created while not belonging to any class. A Java lambda expression will be passed around as if it were 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);
}
}
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 of Java Lambda expression–
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
Therefore, A Java program to demonstrate the working of a 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 functional interfaces in Java with examples. 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 queries regarding Java Functional Interface, feel free to ask in the comment section.
Related Topic- Checked vs Unchecked Exceptions in Java
You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google



