Friday, 26 May 2017

Java Lambda expressions[How to sort the list of product using lambda expression - Comparator]


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

Product.java
class Product
{
    private int id;
    private String name;
    private float price;

    public Product(int id, String name, float price)
    {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
    }

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

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

    public float getPrice()
    {
        return price;
    }

    public void setPrice(float price)
    {
        this.price = price;
    }

}
LambdaDemo.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Java Lambda Expression Example: Comparator
 *
 */
public class LambdaDemo
{

    public static void main(String[] args)
    {
        List<Product> list = new ArrayList<Product>();

        // Adding Products
        list.add(new Product(1, "Sony LED TV", 65000f));
        list.add(new Product(2, "Radio", 3000f));
        list.add(new Product(3, "Laptop", 150000f));

        System.out.println("Before Sorting .... \n");

        displayProductInfo(list);

        System.out.println("\nAfter Sorting ...\n");

        // implementing lambda expression
        Collections.sort(list, (p1, p2) -> {
            return p1.getName().compareTo(p2.getName());
        });

        displayProductInfo(list);
    }

    private static void displayProductInfo(List<Product> list)
    {
        for (Product p : list)
        {
            System.out.println(p.getId() + " " + p.getName() + " "
                    + p.getPrice());
        }
    }

}
Output
Before Sorting .... 

1 Sony LED TV 65000.0
2 Radio 3000.0
3 Laptop 150000.0

After Sorting ...

3 Laptop 150000.0
2 Radio 3000.0
1 Sony LED TV 65000.0
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/LambdaDemo_product_sort_name_App.zip?attredirects=0&d=1

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/a195090f4adc5a52ed7df4d076a2367bfb5b1fbe/BasicJava/LambdaDemo_product_sort_name_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
  • Thursday, 25 May 2017

    Lambda expression[How to filter the list of person using lambda expression - Predicate and Consumer]


    Click here to watch in Youtube :
    https://www.youtube.com/watch?v=GcHBJnaT5RY&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 + "]";
        }
    
    }
    
    LambdaPredicateConsumerDemo.java
    import java.util.Arrays;
    import java.util.List;
    import java.util.function.Consumer;
    import java.util.function.Predicate;
    
    public class LambdaPredicateConsumerDemo
    {
        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---------");
            /*
             * Create a method that prints all people that have name
             * begins with B.
             */
            printNameBeginWith_B(personList,
                    p -> p.getName().startsWith("B"),
                    p -> System.out.println(p));
    
            System.out.println("----------------------------------------");
    
            printNameBeginWith_B(personList,
                    p -> p.getName().startsWith("B"),
                    p -> System.out.println(p.getName()));
    
            System.out.println("----------------------------------------");
            
            printNameBeginWith_B(personList,
                    p -> p.getName().startsWith("B"),
                    p -> System.out.println(p.getAge()));
    
        }
    
        private static void printNameBeginWith_B(List<Person> personList,
                Predicate<Person> predicate, Consumer<Person> consumer)
        {
            for (Person person : personList)
            {
                if (predicate.test(person))
                {
                    consumer.accept(person);
                }
            }
    
        }
    
    }
    
    Output
    ------------Name Starts with B---------
    Person [name=Balu, age=32]
    Person [name=Bharth, age=40]
    ----------------------------------------
    Balu
    Bharth
    ----------------------------------------
    32
    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

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

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/317a84d07ee35ecc7934d034a6ca38f552a21665/BasicJava/LambdaDemo_consumer_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, 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
  • Google+ Followers

    Google+ Badge