Wednesday, 24 May 2017

Lambda expression in java[How to filter the list of person using lambda expression - Predicate]


Click here to watch in Youtube :
https://www.youtube.com/watch?v=6iwQDOAcl6Y&list=UUhwKlOVR041tngjerWxVccw

Person.java
public class Person
{
    private String name;
    private int age;

    public Person(String name, int age)
    {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

    @Override
    public String toString()
    {
        return "Person [name=" + name + ", age=" + age + "]";
    }

}
LambdaPredicateDemo.java
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class LambdaPredicateDemo
{
    public static void main(String[] args)
    {
        List<Person> personList = Arrays.asList(
                new Person("Carla", 33), new Person("Balu", 32),
                new Person("Bharth", 40), new Person("Ajay", 31));

        System.out.println("------------Name Starts with B---------");

        Predicate<Person> predicate = person -> person.getName().startsWith("B");

        printNameBeginWith_B(personList, predicate);

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

    }

    /*
     * Method to print all people that have name begins with B.
     */
    private static void printNameBeginWith_B(List<Person> personList,
                                                Predicate<Person> predicate)
    {
        for (Person person : personList)
        {
            if (predicate.test(person))
            {
                System.out.println(person);
            }
        }

    }

}
Output
------------Name Starts with B---------
Person [name=Balu, age=32]
Person [name=Bharth, age=40]

Refer:
https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html 

https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/317a84d07ee35ecc7934d034a6ca38f552a21665/BasicJava/LambdaDemo_predicate_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
  • Tuesday, 23 May 2017

    Java Tutorial: Lambda expression in java[How to filter the list of person using lambda expression]


    Person.java
    public class Person
    {
        private String name;
        private int age;
    
        public Person(String name, int age)
        {
            super();
            this.name = name;
            this.age = age;
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName(String name)
        {
            this.name = name;
        }
    
        public int getAge()
        {
            return age;
        }
    
        public void setAge(int age)
        {
            this.age = age;
        }
    
        @Override
        public String toString()
        {
            return "Person [name=" + name + ", age=" + age + "]";
        }
    
    }
    
    PersonFilter.java
    @FunctionalInterface
    public interface PersonFilter
    {
        public boolean filter(Person person);
    }
    
    NonLambdaDemo.java
    import java.util.Arrays;
    import java.util.List;
    
    public class NonLambdaDemo
    {
        public static void main(String[] args)
        {
            List<Person> personList = Arrays.asList(
                    new Person("Carla", 33), new Person("Balu", 32),
                    new Person("Bharth", 40), new Person("Ajay", 31));
    
            System.out.println("------------Name Starts with B---------");
    
            PersonFilter personFilter = new PersonFilter()
            {
                @Override
                public boolean filter(Person person)
                {
                    return person.getName().startsWith("B");
                }
            };
    
            printNameBeginWith_B(personList, personFilter);
            System.out.println("---------------------------------");
    
        }
    
        /*
         * Method to print all people that have name begins with B.
         */
        private static void printNameBeginWith_B(List<Person> personList,
                                                       PersonFilter personFilter)
        {
            for (Person person : personList)
            {
                if (personFilter.filter(person))
                {
                    System.out.println(person);
                }
            }
    
        }
    
    }
    
    Output
    ------------Name Starts with B---------
    Person [name=Balu, age=32]
    Person [name=Bharth, age=40]
    ---------------------------------
    
    LambdaDemo.java
    import java.util.Arrays;
    import java.util.List;
    
    public class LambdaDemo
    {
        public static void main(String[] args)
        {
            List<Person> personList = Arrays.asList(
                    new Person("Carla", 33), new Person("Balu", 32),
                    new Person("Bharth", 40), new Person("Ajay", 31));
    
            System.out.println("------------Name Starts with B---------");
            
            PersonFilter personFilter = person -> person.getName().startsWith("B");
            printNameBeginWith_B(personList,personFilter);
    
            System.out.println("----------------------------------------");
    
        }
    
        /*
         * Method to print all people that have name begins with B.
         */
        private static void printNameBeginWith_B(List<Person> personList,
                                                                PersonFilter personFilter)
        {
            for (Person person : personList)
            {
                if (personFilter.filter(person))
                {
                    System.out.println(person);
                }
            }
    
        }
    
    }
    
    Output
    ------------Name Starts with B---------
    Person [name=Balu, age=32]
    Person [name=Bharth, age=40]
    ----------------------------------------
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/LambdaDemo_filter_person_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/c1aa2efd864c22fbec84424255460b099194034f/BasicJava/LambdaDemo_filter_person_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
  • Lambda expression in java[How to sort the list of person by name using lambda expression]

    Person.java
    public class Person
    {
        private String name;
        private int age;
    
        public Person(String name, int age)
        {
            super();
            this.name = name;
            this.age = age;
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName(String name)
        {
            this.name = name;
        }
    
        public int getAge()
        {
            return age;
        }
    
        public void setAge(int age)
        {
            this.age = age;
        }
    
        @Override
        public String toString()
        {
            return "Person [name=" + name + ", age=" + age + "]";
        }
    
    }
    
    NonLambdaDemo.java
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    /**
     * Before JDK 8
     * 
     * With out lambda expression how to sort the list of person by name
     * 
     */
    public class NonLambdaDemo
    {
        public static void main(String[] args)
        {
            List<Person> personList = Arrays.asList(
                    new Person("Carla", 33), new Person("Balu", 32),
                    new Person("Bharth", 40), new Person("Ajay", 31));
    
            System.out.println("------------Before Sort by name---------");
    
            printAll(personList);
    
            System.out.println("----------------------------------------");
    
            /*
             * Sort list by Name,Before JDK 8
             */
    
            Collections.sort(personList, new Comparator<Person>()
            {
                @Override
                public int compare(Person p1, Person p2)
                {
                    return p1.getName().compareTo(p2.getName());
                }
            });
    
            System.out.println("\n------------After Sort by name---------");
            /*
             * Create a method that prints all elements in the list.
             */
    
            printAll(personList);
    
            System.out.println("----------------------------------------");
    
        }
    
        private static void printAll(List<Person> personList)
        {
            for (Person person : personList)
            {
                System.out.println(person);
            }
    
        }
    }
    
    Output
    ------------Before Sort by name---------
    Person [name=Carla, age=33]
    Person [name=Balu, age=32]
    Person [name=Bharth, age=40]
    Person [name=Ajay, age=31]
    ----------------------------------------
    
    ------------After Sort by name---------
    Person [name=Ajay, age=31]
    Person [name=Balu, age=32]
    Person [name=Bharth, age=40]
    Person [name=Carla, age=33]
    ----------------------------------------
    
    LambdaDemo.java
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    
    /**
     * JDK 8
     * 
     * With lambda expression how to sort the list of person by name.
     * 
     */
    
    public class LambdaDemo
    {
        public static void main(String[] args)
        {
            List<Person> personList = Arrays.asList(
                    new Person("Carla", 33), new Person("Balu", 32),
                    new Person("Bharth", 40), new Person("Ajay", 31));
    
            System.out.println("------------Before Sort by name---------");
    
            printAll(personList);
    
            System.out.println("----------------------------------------");
    
            /*
             * Sort list by Name, with JDK 8
             */
    
            Collections.sort(personList,
                    (p1, p2) -> p1.getName().compareTo(p2.getName()));      
            
            
            System.out.println("\n------------After Sort by name---------");
            /*
             * Create a method that prints all elements in the list.
             */
    
            printAll(personList);
    
            System.out.println("----------------------------------------");
    
        }
    
        private static void printAll(List<Person> personList)
        {
            for (Person person : personList)
            {
                System.out.println(person);
            }
    
        }
    }
    
    Output

    ------------Before Sort by name---------
    Person [name=Carla, age=33]
    Person [name=Balu, age=32]
    Person [name=Bharth, age=40]
    Person [name=Ajay, age=31]
    ----------------------------------------
    
    ------------After Sort by name---------
    Person [name=Ajay, age=31]
    Person [name=Balu, age=32]
    Person [name=Bharth, age=40]
    Person [name=Carla, age=33]
    ----------------------------------------
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/LambdaDemo_sort_pl_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/c1aa2efd864c22fbec84424255460b099194034f/BasicJava/LambdaDemo_sort_pl_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
  • Friday, 19 May 2017

    Kids: Health, Household and Personal Care_V15

    Kids: Health, Household and Personal Care_V11

    Java Tutorial: Lambda expression in java[Structure of Lambda expression_V2]


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

    Click the below Image to Enlarge
    Java Tutorial: Lambda expression in java[Structure of Lambda expression_V2] 
    Java Tutorial: Lambda expression in java[Structure of Lambda expression_V2] 
    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
  • Java Tutorial: Lambda expression in java[Structure of Lambda expression_V1]

    Thursday, 18 May 2017

    Java Tutorial: Lambda expression in java[Thread creation using Lambda expression]


    Click here to watch in Youtube :
    https://www.youtube.com/watch?v=4qHFrzYJ1XU&list=UUhwKlOVR041tngjerWxVccw

    LambdaDemo.java
    /**
     * With JDK 8
     * 
     * Java Lambda Expression Example: Creating Thread
     *
     */
    public class LambdaDemo
    {
    
        public static void main(String[] args)
        {
            /*
             * Thread Example with lambda.
             * 
             * we are implementing run method by using lambda expression.
             */
    
            Runnable runnable = () -> {
                System.out.println("Thread is running...");
            };
            Thread thread = new Thread(runnable);
            thread.start();
        }
    
    }
    
    Output
    Thread is running...
    
    NonLambdaDemo.java
    /**
     * Before JDK 8
     *
     */
    public class NonLambdaDemo
    {
    
        public static void main(String[] args)
        {
            // Thread Example without lambda
            Runnable runnable = new Runnable()
            {
                @Override
                public void run()
                {
                    System.out.println("Thread is running...");
                }
            };
            Thread thread = new Thread(runnable);
            thread.start();
        }
    
    }
    
    
    Output
    Thread is running...
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/LambdaDemo_thread_create_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/539015917c8406e4be671410d713073b3971d1a5/BasicJava/LambdaDemo_thread_create_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
  • Java Tutorial: Lambda expression in java[Lambda expression with multiple statements]


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

    LambdaDemo.java
    @FunctionalInterface
    interface Message
    {
        String displayMessage(String strValue);
    }
    
    /**
     * Java Lambda Expression Example: Multiple Statements
     *
     */
    public class LambdaDemo
    {
    
        public static void main(String[] args)
        {
            /*
             * We can pass multiple statements in lambda expression.
             */
            Message msg = (strValue) -> {
                String str1 = "Welcome to ";
                String str2 = str1 + strValue;
                return str2;
            };
            System.out.println(msg.displayMessage("India."));
        }
    
    }
    
    Output
    Welcome to India.
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/LambdaDemo_multi_statement_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/539015917c8406e4be671410d713073b3971d1a5/BasicJava/LambdaDemo_multi_statement_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
  • Java Tutorial: Lambda expression in java | Java Lambda expressions[Lambda expression-foreach loop]


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

    LambdaDemo.java
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * 
     * Java Lambda Expression Example: For-each Loop
     *
     */
    public class LambdaDemo
    {
    
        public static void main(String[] args)
        {
            List<String> nameList = new ArrayList<String>();
            nameList.add("Ram");
            nameList.add("Peter");
            nameList.add("John");
    
            nameList.forEach((name) -> System.out.println(name));
        }
    
    }
    
    Output
    Ram
    Peter
    John
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/LambdaDemo_for_each_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/539015917c8406e4be671410d713073b3971d1a5/BasicJava/LambdaDemo_for_each_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
  • Java Tutorial: Lambda expression in java | Java Lambda expressions[Method accept Lambda expression]


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

    LambdaDemo.java
    @FunctionalInterface
    interface StringLengthLambda
    {
        int getLength(String str);
    }
    
    public class LambdaDemo
    {
    
        public static void main(String[] args)
        {
            printLambda(str -> str.length());
        }
    
        private static void printLambda(StringLengthLambda stringLengthLambda)
        {
            int length = stringLengthLambda.getLength("Welcome");
            System.out.println("length = " + length);
        }
    
    }
    
    Output
    length = 7
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/LambdaDemo_method_accept_lambda_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/539015917c8406e4be671410d713073b3971d1a5/BasicJava/LambdaDemo_method_accept_lambda_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
  • Java Tutorial: Lambda expression in java[Lambda expression example-string length]


    Click here to watch in Youtube : 
    https://www.youtube.com/watch?v=42dISsjfHPs&list=UUhwKlOVR041tngjerWxVccw

    LambdaDemo.java
    @FunctionalInterface
    interface StringLengthLambda
    {
        int getLength(String str);
    }
    
    public class LambdaDemo
    {
    
        public static void main(String[] args)
        {
            StringLengthLambda stringLengthLambda = (String str) -> str.length();
            int length = stringLengthLambda.getLength("Welcome");
            System.out.println("length = " + length);
        }
    
    }
    
    Output
    length = 7
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/LambdaDemo_str_length_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/539015917c8406e4be671410d713073b3971d1a5/BasicJava/LambdaDemo_str_length_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
  • Wednesday, 17 May 2017

    Java Tutorial: Lambda expression in java[Lambda expression example with return keyword]


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

    LambdaDemo.java
    @FunctionalInterface
    interface Add
    {
        int addition(int a, int b);
    }
    
    /**
     * 
     * You must use return keyword when lambda expression contains
     * multiple statements.
     * 
     * Java Lambda Expression Example: With return keyword
     *
     */
    public class LambdaDemo
    {
    
        public static void main(String[] args)
        {
            // Lambda expression with return keyword.
            Add add = (int a, int b) ->
            {
                int sum = a + b;
                return sum;
            };
    
            int sum = add.addition(13, 53);
            System.out.println("sum = " + sum);
        }
    
    }
    
    Output
    sum = 66
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/LambdaDemo_with_return_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/539015917c8406e4be671410d713073b3971d1a5/BasicJava/LambdaDemo_with_return_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
  • Java Tutorial: Lambda expression in java[Lambda expression example without return keyword]


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

    LambdaDemo.java
    @FunctionalInterface
    interface Add
    {
        int addition(int a, int b);
    }
    
    /**
     * 
     * In Java lambda expression, if there is only one statement, you may
     * or may not use return keyword.
     * 
     * Java Lambda Expression Example: Without return keyword
     *
     */
    public class LambdaDemo
    {
    
        public static void main(String[] args)
        {
            // Lambda expression without return keyword.
            Add add = (a, b) -> (a + b);
    
            int sum = add.addition(10, 20);
            System.out.println("sum = " + sum);
        }
    
    }
    
    Output
    sum = 30
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/LambdaDemo_without_return_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/539015917c8406e4be671410d713073b3971d1a5/BasicJava/LambdaDemo_without_return_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
  • Google+ Followers

    Google+ Badge