Functional Programming in Java Using Lambda Expressions
A lambda expression is a short and concise way to represent an anonymous function—a function without a name .
Key Features
- No method name
- No return type (inferred by compiler)
- Used with functional interfaces
- Reduces boilerplate code

1.Why Lambda expression required
Lambda expression required to enable functional programming ,lambda expression allow you to define small functions without naming them . this is useful when the function is simple and used only once or a few times .
Lambda Syntax :
(parameters) -> {statements}
- Remove modifier , return type , and method Name .
- Lambda expression can take any number of parameters , if multiple parameters present . then these parameters should be separated with comma .
2.Lambda Expression Example
Example 1: Addition of Number
Without Lambda Expressions
public void add(int a , int b)
{
System.out.println(a+b);
}
With Lambda Expressions
(a,b) -> {System.out.println(a+b)}
Example 2: Find the length of String
without Lambda Expressions
public int length(String s){
System.out.pintln(s.length);
return s.length;
}
With Lambda Expression
(s) -> {System.out.println(s.length)}
if single statements are there then curly bracket not required , if body contained multiple lines of code then curly bracket required . if lambda expressions return something then we can return keywords .
What is Functional Interface
A Functional Interface that contains exactly one abstract method . and any number of default and static method can contains .
What is the purpose of Functional Interface
The purpose of functional interface in java is to provide a target for lambda expressions ,enabling functional programming within the language . Functional interfaces simplify the development process by allowing you to write more concise and readable code.
Key purpose of Functional Interface
1. Enable Lambda Expressions
Functional interfaces are designed to work with lambda expressions, which provide a clear and concise way to represents instance of single method interfaces . Functional interfaces are the foundation of lambda expression in java .
@Functionalinterface
interface MyFunction{
void apply(String s)
}
MyFunction func =(s) -> System.out.println(s);
func.apply("Hello");
2. Functional Interface with Interfaces
if an interface extends functional interface and child interface does not contain abstract method then child is always functional interface .
@FunctionalInterface
Interface PaymentInterface
{
public void m1();
public void m2(){}
public void m3(){}
}
Functional Interface with Inheritance
Case-1 : if an Interface extends functional interface and child interface does not contain any abstract method , the child interface is always a functional interface .
@FunctionalInterface
Interface ParentInterface
{
public void m1();
}
@FunctionalInterface
Interface ChildInterface extends ParentInterface
{
}
Case-2 : if the child Functional Interface , define exactly same parent interface abstract method then it will work fine .
@FunctionalInterface
Interface ParentInterface
{
public void m1();
}
@FunctionalInterface
Interface childInterface extends ParentInterface
{
public void m1();
}
Case-3: In the child Functional Interface we can not define any new abstract method otherwise we will get compile time error .
@FunctionalInterface
Public Interface PaymentInterface
{
public void m1();
}
@FunctionalInterface
public Interface childpayment extends paymentInterface
{
public void m2();
}
Once you compile the above code you will get Invalid @functionalInterface annotation childpayment is not a functional interface .
Case-4: Normal Interface Can extends functional interface
@FunctionalInterface
public interface parentinterface
{
public void m1();
}
public interface childInterface extends parentInterface
{
public void m2();
}
Invoke Lambda Expression by using Functional Interface
@FunctionalInterface
Interface Funca
{
public void m1();
}
public class MainTest
{
public static void main(String[] args)
{
Funca obj= () -> System.out.println("m1 method Implementation");
obj.m1();
}
}
Now we will see some example based on the above concepts .
program 1: write a program to find the addition of two number
@FunctionalInterface
interface Calculation
{
public void add(int a, int b)
}
class Demotest
{
public static void Main(String[] args){
Calculation cal= (a,b) -> System.out.println("sum of a+b is "+(a+b));
cal.add(10,20);
cal.add(20,30);
}
}
Program-2: Write a program to find the Square of given Number
@FunctionalInterface
interface Square{
public int square(int a);
}
public class TestDemo{
public static void main(String[] args){
Square sq=x -> x*x ;
system.out.println(sq.square(4));
}
}
Program-3 : write a program to create a Thread Using Lambda Expression
package com.test.rkdigital.school.solid;
import java.util.Iterator;
public class ThreadDemo {
public static void main(String[] args) {
Runnable r= ()-> {
for(int i=0;i<=10;i++) {
System.out.println("child thread !");
}
};
Thread t=new Thread(r);
t.start();
for (int i = 0; i < 10; i++) {
System.out.println("Main Thread!!!");
}
}
}