Tuesday, 30 May 2017

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


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

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/LambdaDemo_list_foreach_app

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/4c13bd19ff03c6acdc5e87d7803290d625ce640a/BasicJava/LambdaDemo_list_foreach_app/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge