0% found this document useful (0 votes)
27 views

Lambda Expression

Lambda expressions are anonymous functions that can be passed around as method arguments in Java 8. A lambda expression represents a functional interface and can be used to provide an implementation. Lambda expressions allow for more concise code by removing the need to explicitly define named classes or methods. Examples show lambda expressions with zero, one, or multiple parameters that implement interfaces to print output or perform operations like addition.

Uploaded by

avinash.abhi887
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Lambda Expression

Lambda expressions are anonymous functions that can be passed around as method arguments in Java 8. A lambda expression represents a functional interface and can be used to provide an implementation. Lambda expressions allow for more concise code by removing the need to explicitly define named classes or methods. Examples show lambda expressions with zero, one, or multiple parameters that implement interfaces to print output or perform operations like addition.

Uploaded by

avinash.abhi887
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lambda Expression

Lambda expression is a new and important feature of Java included in Java SE 8.

A lambda expression in Java is a concise way to represent an anonymous


function,anonymous function is a function that does not have a name and is
defined inline, typically at the point where it is needed.
The Lambda expression is used to provide the implementation of functional
interface.

Example and syntax of lambda expression:

code example of a lambda expression with 0 input:

interface A{

void show();

public class Main {

public static void main(String[] args) {

A a = () -> {

Lambda Expression 1
System.out.println("calling");
};

// or

A a = () -> System.out.println("calling");

a.show();

Code example of lambda interface with one input:

interface A{

void show(int a);

public class Main {

public static void main(String[] args) {

A a = (inp) -> {
System.out.println(inp);
};

Lambda Expression 2
// or

A a = (inp) -> System.out.println(inp);

a.show(10);

Code example of lambda interface with multiple input:

interface A{

int add(int a, int b);

public class Main {

public static void main(String[] args) {

// A a = (i1,i2) -> {
//
// return i1+i2;
// };

Lambda Expression 3
A a = (i1,i2)-> i1+i2; // if we had multiple lines th
//

A a = (i1,i2) -> {
if(i1+i2 > 10){
return 1;
}else{
return 0;
}
};

System.out.println(a.add(5,6));

Example of using a lambda expression to sort objects:

public class BookService {

public List<Book> getBooksinSort() {


List<Book> books = new BookDAO().getBooks();
Collections.sort(books, (o1, o2) -> o1.getName().compare

Lambda Expression 4
return books;
}

public static void main(String[] args) {


System.out.println(new BookService().getBooksinSort());
}
}

/*
* class MyComparator implements Comparator<Book> {
*
* @Override public int compare(Book o1, Book o2) { return
* o2.getName().compareTo(o1.getName()); }
*/

Lambda Expression 5

You might also like