• Breaking News

    Sunday, 20 September 2015

    Java-8 Lambda Expression Tutorial - Iterate Over List Using Lambda Expression

    Iterate Over List Using Lambda Expression

    In Normal way we iterate over list using for each loop But Java 8 provide another way to iterate over list using lambda expression.

    package com.kodemaker;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class IterateListUsingLambdaExpression {
     
     public static void main(String[] args) {
       
       List<String> iterateList = new ArrayList<String>();
       iterateList.add("1");
       iterateList.add("2");
       iterateList.add("3");
       iterateList.add("4");
       iterateList.add("5");
       
       System.out.println("Simple for each example");
       
       for(String currentElement :iterateList){
        System.out.println("Item in List "+currentElement);
       }
       
       System.out.println("for each example using lambda expression");
       
       iterateList.forEach(currentElement ->System.out.println("Item in List "+currentElement ));
     }
     
    }
    


    Simple for each example                                                                                                                                                  
    Item in List 1                                                                                                                                                           
    Item in List 2                                                                                                                                                           
    Item in List 3                                                                                                                                                           
    Item in List 4                                                                                                                                                           
    Item in List 5                                                                                                                                                           
    for each example using lambda expression                                                                                                                                 
    Item in List 1                                                                                                                                                           
    Item in List 2                                                                                                                                                           
    Item in List 3                                                                                                                                                           
    Item in List 4                                                                                                                                                           
    Item in List 5 
    

    No comments:

    Post a Comment