Thursday, 8 June 2017

Java Tutorial: Lambda expression in java | Java Lambda expressions[forEach method of List] ~ foundjava


Click here to watch in Youtube :
https://www.youtube.com/watch?v=Xv5j2bgX-B4&list=UUhwKlOVR041tngjerWxVccw



Click the below Image to Enlarge

Java Tutorial: Lambda expression in java | Java Lambda expressions[forEach method of List] 
LambdaDemo1.java
import java.util.ArrayList;
import java.util.List;

public class LambdaDemo1
{

    public static void main(String[] args)
    {

        List<String> nameList = new ArrayList<>();
        nameList.add("Peter");
        nameList.add("John");
        nameList.add("Juli");
        nameList.add("Stephan");

        /*
         * Before Java 8, Normal way to loop a list.
         */
        for (String name : nameList)
        {
            System.out.println(name);
        }

        System.out.println("------------------------------------");

        /*
         * In Java 8, we can loop a List with forEach + lambda
         * expression or method reference.
         */
        nameList.forEach(name -> System.out.println(name));

    }

}
Output
Peter
John
Juli
Stephan
------------------------------------
Peter
John
Juli
Stephan
LambdaDemo2.java
import java.util.ArrayList;
import java.util.List;

public class LambdaDemo2
{

    public static void main(String[] args)
    {

        List<String> nameList = new ArrayList<>();
        nameList.add("Peter");
        nameList.add("John");
        nameList.add("Juli");
        nameList.add("Stephan");

        /*
         * In Java 8, we can loop a List with forEach + lambda
         * expression or method reference.
         */

        nameList.forEach(name -> {
            if ("John".equals(name))
            {
                System.out.println("Hello " + name);
            }
        });
    }

}
Output
Hello John
LambdaDemo3.java
import java.util.ArrayList;
import java.util.List;

public class LambdaDemo3
{

    public static void main(String[] args)
    {

        List<String> nameList = new ArrayList<>();
        nameList.add("Peter");
        nameList.add("John");
        nameList.add("Juli");
        nameList.add("Stephan");

        /*
         * In Java 8, we can loop a List with forEach + lambda
         * expression or method reference.
         */

        //method reference
        nameList.forEach(System.out::println);
    }

}
Output
Peter
John
Juli
Stephan
LambdaDemo4.java
import java.util.ArrayList;
import java.util.List;

public class LambdaDemo4
{

    public static void main(String[] args)
    {

        List<String> nameList = new ArrayList<>();
        nameList.add("Peter");
        nameList.add("John");
        nameList.add("Juli");
        nameList.add("Stephan");

        /*
         * In Java 8, we can loop a List with forEach + lambda
         * expression or method reference.
         */

        //Stream and filter
        nameList.stream()
            .filter(name->name.contains("Juli"))
            .forEach(System.out::println);
    }

}
Output
Juli
Refer 
https://docs.oracle.com/javase/8/docs/api/index.html?java/lang/Iterable.html

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/LambdaDemo_list_foreach_app.zip?attredirects=0&d=1

No comments:

Post a Comment