0% found this document useful (0 votes)
5 views2 pages

Java 8 Lambda Expressions Overview

Java Lambda Expressions were introduced in Java 8 as a concise way to represent a block of code that takes parameters and returns a value. They can be implemented directly within methods and are similar to methods but do not require a name. The document includes examples demonstrating the use of lambda expressions with ArrayLists to print elements and filter even numbers.

Uploaded by

Shikha Singh
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)
5 views2 pages

Java 8 Lambda Expressions Overview

Java Lambda Expressions were introduced in Java 8 as a concise way to represent a block of code that takes parameters and returns a value. They can be implemented directly within methods and are similar to methods but do not require a name. The document includes examples demonstrating the use of lambda expressions with ArrayLists to print elements and filter even numbers.

Uploaded by

Shikha Singh
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

Java Lambda Expressions

Lambda Expressions were added in Java 8.


A lambda expression is a short block of code which takes in parameters and
returns a
value. Lambda expressions are similar to methods, but they do not need a name
and they
can be implemented right in the body of a method.

import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
[Link](5);
[Link](9);
[Link](8);
[Link](1);
[Link]( (n) -> { [Link](n); } );
}
}

import [Link];
import [Link];

public class Main {


public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
[Link](5);
[Link](9);
[Link](8);
[Link](1);
Consumer<Integer> method = (n) -> { [Link]

Java Lambda Expressions 1


(n); };
[Link]( method );
}
}

// A Java program to demonstrate simple lambda expressions


import [Link];
class Test {
public static void main(String args[])
{
// Creating an ArrayList with elements
// {1, 2, 3, 4}
ArrayList<Integer> arrL = new ArrayList<Integer>();
[Link](1);
[Link](2);
[Link](3);
[Link](4);

// Using lambda expression to print all elements


// of arrL
[Link](n -> [Link](n));

// Using lambda expression to print even elements


// of arrL
[Link](n -> {
if (n % 2 == 0)
[Link](n);
});
}
}

Java Lambda Expressions 2

You might also like