facebook

How to use Lambda expression in Java 8

By Suchandra Mondal

How to use Lambda expression in Java 8

Lambda Expression in Java 8- Lambda expression basically expresses instances of functional interfaces. Lambda expression is a new feature of Java 8. It adds more functionalities like treat functionality as a method with arguments, a function can be created without belonging to any class. The lambda expression is containing two parts the one on the left of the arrow symbol (->) having its parameters and the one on the right containing its body of this function.
Syntax:
lambda operator -> body
Zero parameter:
() -> System.out.println(“Zero parameter lambda”);
One parameter:–
(p) -> System.out.println(“One parameter: ” + p);
Multiple parameters :
(parameter1, parameter2) -> System.out.println(” parameters : ” + parameter1 + “, ” + parameter2);
// A Java program to demonstrate lambda expressions
import java.util.ArrayList;
class Test
{
public static void main(String args[])
{
// Creating an ArrayList with integer elements
// {1, 2, 3, 4}
ArrayList<Integer> integerArr = new ArrayList<Integer>();
integerArr.add(1);
integerArr.add(2);
integerArr.add(3);
integerArr.add(4);

// Using lambda expression to print all elements
// of integerArr
integerArr.forEach(num -> System.out.println(num));

// Using lambda expression to print even elements
// of integerArr
integerArr.forEach(n -> { if (num%2 == 0) System.out.println(num); });
}
}

// Java program to demonstrate working of lambda expressions using interface and argument
public class Example
{
// operation is implemented using lambda expressions
interface InterfaceFirst
{
int result(int a, int b);
}

// welcomeMessage() is implemented using lambda expressions
// above
interface InterfaceSecond
{
void welcomeMessage(String message);
}

// Performs InterfaceFirst’s operation on ‘a’ and ‘b’
private int operate(int a, int b, InterfaceFirst fobj)
{
return fobj.result(a, b);
}

public static void main(String args[])
{
// lambda expression for addition for two parameters
// This expression implements ‘InterfaceFirst’ interface
InterfaceFirst add = (int x, int y) -> x + y;

// lambda expression multiplication for two parameters
// This expression also implements ‘InterfaceFirst’ interface
InterfaceFirst mul = (int x, int y) -> x * y;

// Creating an object of Example to call operate using
// different implementations using lambda Expressions
Example testObj = new Example();

// Add two numbers using lambda expression
System.out.println(“Addition is ” +
testObj.result(6, 3, add));

// Multiply two numbers using lambda expression
System.out.println(“Multiplication is ” +
testObj.result(6, 3, mul));

// lambda expression for single parameter
// This expression implements ‘FuncInter2’ interface
InterfaceSecond sobj = message ->System.out.println(“Welcome “
+ message);
sobj.welcomeMessage(“Java”);
}
}

Suchandra Mondal Subscriber
Android Developer , Openweb Solutions

Android Developer at Openweb Solutions

Posts created 5

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top
shares