Lambda Expressions in Java 8

Posted on July 30, 2019
lambda expression
java 8
43
import java.util.ArrayList; 
class Test 
{ 
    public static void main(String args[]) 
    { 
        // Creating an ArrayList with elements  {1, 2, 3, 4} 
        ArrayList<Integer> arrL = new ArrayList<Integer>(); 
        arrL.add(1); 
        arrL.add(2); 
        arrL.add(3); 
        arrL.add(4); 
  
        // Using lambda expression to print all elements of arrL 
        arrL.forEach(n -> System.out.println(n)); 
  
        // Using lambda expression to print even elements of arrL 
        arrL.forEach(n -> { if (n%2 == 0) System.out.println(n); }); 
    } 
} 

 




0 comments

Please log in to leave a comment.

Author
Reshma

13 posts

Share this