Site icon JavaGoal

Lambda expression java 8

In this post, we will see what is the lambda expression java. In java, Lambda expression was introduced in java 8. In java 8 lambda expression changes the way the coding and provides some tips to write efficient code. It is also known as functional programming java 8

Here is the table content of the article will we will cover this topic.
1. What is Lambda expression in Java 8?
2. Syntax of Lambda expression in Java8?
3. Rules in Lambda expression?
4. How to use Lambda expression?
5. How does lambda work in java?
6. Java 8 lambda expression example?
7. Why Lambda expression uses a functional interface only?
8. Lambda expression with the return statement?

What is a lambda expression Java?

Lambda expressions are new features for JAVA, but it is already existing in Some other popular programming languages like Scala.
Java 8 onwards supports functional programming. Because now you can pass a function as a parameter in another function. It is an anonymous function(A function without a name). Java has been lacking this from the beginning. Let’s discuss it in detail.

Syntax of Lambda expression in Java8

(parameters) -> { statements; }

Round brackets (): It contains the parameters. The round brackets are mandatory.
Parameters: The parameter can be empty or non-empty as well. These parameters can use in the statement.
Arrow token ->: It is mandatory it’s just a symbol that links the parameters and statements.
Curly braces { }: These are not mandatory if you have one line of the statement.
Statements: You can place the statement or block of code.

Here we are creating valid syntax with examples:

( ) -> { statements; }
()-> { System.out.println("Hi!!!!!"); }

( ) ->  statements; 
( ) ->  System.out.println("Hi!!!!!");

(parameters) -> { statements; }
(int a, int b) -> { System.out.println(a+b); }

(parameters) ->  statements; 
(int a, int b) ->  System.out.println(a+b); 

Rules in Lambda expression

  1. Lambda expression can have any number of parameters. The parameters are not mandatory. They can be empty or not empty.
  2. The type of parameters should be matched with the type of the parameter of the abstract method.
  3. The round brackets are not mandatory if the lambda expression has a single parameter. But when there is more than one parameter then it is mandatory.
  4. The body of the lambda expressions can contain any number of statements.
  5. If the body of the lambda expression has a single statement, the curly brackets are not mandatory but when there is more than one statement in the body then these must be enclosed in curly brackets.

How to use Java 8 lambda expressions

Lambda expression works with a Functional interface. Basically, lambda expression provides the implementation of a Functional interface. It represents the instance of the Functional Interface.

@FunctionalInterface
interface ExampleOfFunctionInterface 
{
	public void firstMethod();
}

To understand the working of Lambda we need to understand existing approaches that are used to implement the interface.

public class FirstApprochToImplement implements ExampleOfFunctionInterface
{
	public static void main(String[] args) 
	{
		FirstApprochToImplement object = new FirstApprochToImplement();
		System.out.println("In this class we are implementing the functinal interface"); 
		object.firstMethod();
	}

	public void firstMethod()
	{
		System.out.println("Function interface can contains only one abstract method");
		System.out.println("Providing body to abstract method");
	}

}

Output: In this class we are implementing the functional interface
Function interface can contains only one abstract method
Providing body to abstract method

Here we are implementing the interface and providing the body to the method. After that, we are creating an object to access the method. If you do not know this approach, please read it from here.

public class SecondApprochToImplement 
{
	public static void main(String[] args) 
	{
		ExampleOfFunctionInterface object =  new ExampleOfFunctionInterface() 
		{
			@Override
			public void firstMethod() 
			{
				System.out.println("Function interface can contains only one abstract method");
			}
		};
		System.out.println("In this class we are provide body to method during the creation of object");
		object.firstMethod();
	}
}

Output: In this class we are provide body to method during the creation of object
Function interface can contains only one abstract method

Here we are not implementing the interface. We are creating the object of the Interface so we must have to provide the body to the method. After that, we are accessing the method. If you do not know this approach, please read it from here.

By use of  Lambda expression user neither need to implement the interface nor needs to create the anonymous class:

public class ThirdApprochToImplement 
{
	public static void main(String[] args) 
	{
		ExampleOfFunctionInterface object =  () -> 
		{
				System.out.println("Function interface can contains only one abstract method");
		};
		
		System.out.println("In this class we are provide body to method by use of Lambda expressions.");
		object.firstMethod();
	}
}

Output: In this class we are provide body to method by use of Lambda expressions.
Function interface can contains only one abstract method

Let’s take another example of Lambda expression

import java.util.ArrayList;
public class LibraryRecords 
{
	public static void main(String[] args) 
	{
		ArrayList<Library> listOfBooks = new ArrayList<Library>();
		listOfBooks.add(new Library(12, "Java begin", "PK mohan"));
		listOfBooks.add(new Library(22, "Logical", "RK"));
		listOfBooks.add(new Library(14, "Mathematics", "Rama"));
		listOfBooks.add(new Library(18, "Java CORE", "Krishana"));
		listOfBooks.add(new Library(54, "Data structutre", "MS"));
		
		listOfBooks.stream()
				.filter(book -> book.getBookId() < 20)
				.forEach(book -> 
				{
					System.out.println("Book Name = "+book.getBookName());
					System.out.println("Book Id = "+book.getBookId());
				});
	}

}

Output: Book Name = Java begin
Book Id = 12
Book Name = Mathematics
Book Id = 14
Book Name = Java CORE
Book Id = 18

Exit mobile version