Thursday, 31 August 2017

How to filter a Map and return a filtered Map using Java 8 streams | Streams in Java 8


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

StreamDemo.java
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * Example to filter a Map and return a map.
 */
public class StreamDemo
{
    public static void main(String[] args)
    {

        Map<Integer, String> fruitsMap = new HashMap<>();
        fruitsMap.put(1, "Apple");
        fruitsMap.put(2, "Orange");
        fruitsMap.put(3, "Banana");

         //Map -> Stream -> Filter -> Map
        Map<Integer, String> filteredMap = fruitsMap.entrySet().stream()
                .filter(map -> map.getKey() == 2)
                .collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));

        System.out.println(filteredMap);

    }

}
Output
{2=Orange}

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/53e39342c1c41555c6e548783b3cf96ab7bd9ee6/BasicJava/StreamDemo_filter_fruits_map_return_map/?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
  • How to filter a Map and return a String value using Java 8 streams | Streams in Java 8


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

    StreamDemo.java
    import java.util.HashMap;
    import java.util.Map;
    import java.util.stream.Collectors;
    
    /**
     * 
     * Example to filter a Map and return a value.
     */
    public class StreamDemo
    {
        public static void main(String[] args)
        {
    
            Map<Integer, String> fruitsMap = new HashMap<>();
            fruitsMap.put(1, "Apple");
            fruitsMap.put(2, "Orange");
            fruitsMap.put(3, "Banana");
    
            String result = "";
            for (Map.Entry<Integer, String> entry : fruitsMap.entrySet())
            {
                if (2 == entry.getKey())
                {
                    result = entry.getValue();
                }
            }
            System.out.println("Before Java 8 : " + result);
    
            // Map -> Stream -> Filter -> String
            result = fruitsMap.entrySet().stream()
                    .filter(map -> 2 == map.getKey())
                    .map(map -> map.getValue())
                    .collect(Collectors.joining());
    
            System.out.println("With Java 8 : " + result);
    
        }
    
    }
    
    Output
    Before Java 8 : Orange
    With Java 8 : Orange
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_filter_fruits_map.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/53e39342c1c41555c6e548783b3cf96ab7bd9ee6/BasicJava/StreamDemo_filter_fruits_map/?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
  • How to convert list of sw engineer objects into a list of employee objects using Java 8 streams


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

    Employee.java
    public class Employee
    {
        private String name;
        private int age;
    
        public Employee()
        {
    
        }
    
        public Employee(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 "Employee [name=" + name + ", age=" + age + "]";
        }
    
    }
    
    SoftwareEngineer.java
    public class SoftwareEngineer
    {
        private String name;
        private int age;
    
        public SoftwareEngineer(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;
        }
    
    }
    
    NonStreamDemo.java
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    /**
     * 
     * List of objects -> List of other objects
     * 
     * This example shows you how to convert a list of SoftwareEngineer
     * objects into a list of Employee objects.
     */
    public class NonStreamDemo
    {
        public static void main(String[] args)
        {
    
            List<SoftwareEngineer> softwareEngineerList = Arrays.asList(
                    new SoftwareEngineer("Ram", 30),
                    new SoftwareEngineer("Peter", 27),
                    new SoftwareEngineer("Dave", 33));
    
            // Before java 8
            List<Employee> employeeList = convertToEmployee(softwareEngineerList);
            System.out.println(employeeList);
        }
    
        private static List<Employee> convertToEmployee(List<SoftwareEngineer> softwareEngineerList)
        {
    
            List<Employee> employeeList = new ArrayList<>();
    
            for (SoftwareEngineer softwareEngineer : softwareEngineerList)
            {
                Employee employee = new Employee();
                employee.setName(softwareEngineer.getName());
                employee.setAge(softwareEngineer.getAge());
                employeeList.add(employee);
            }
    
            return employeeList;
    
        }
    
    }
    
    Output
    [Employee [name=Ram, age=30], Employee [name=Peter, age=27], Employee [name=Dave, age=33]]
    
    
    StreamDemo.java
    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;
    
    /**
     * 
     * List of objects -> List of other objects
     * 
     * This example shows you how to convert a list of SoftwareEngineer
     * objects into a list of Employee objects.
     */
    public class StreamDemo
    {
        public static void main(String[] args)
        {
    
            List<SoftwareEngineer> softwareEngineerList = Arrays.asList(
                    new SoftwareEngineer("Ram", 30),
                    new SoftwareEngineer("Peter", 27),
                    new SoftwareEngineer("Dave", 33));
    
            // Java 8
            List<Employee> employeeList = softwareEngineerList.stream()
                    .map(softwareEngineer -> {
                        Employee employee = new Employee();
                        employee.setName(softwareEngineer.getName());
                        employee.setAge(softwareEngineer.getAge());
                        return employee;
                        }).collect(Collectors.toList());
    
            System.out.println(employeeList);
        }
    
    }
    
    Output
    [Employee [name=Ram, age=30], Employee [name=Peter, age=27], Employee [name=Dave, age=33]]
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_swl_to_empl_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/53e39342c1c41555c6e548783b3cf96ab7bd9ee6/BasicJava/StreamDemo_swl_to_empl_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
  • How to get name values from a list of Software engineer objects using Java 8 streams


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

    SoftwareEngineer.java
    public class SoftwareEngineer
    {
        private String name;
        private int age;
        private int salary;
    
        public SoftwareEngineer(String name, int age, int salary)
        {
            super();
            this.name = name;
            this.age = age;
            this.salary = salary;
        }
    
        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;
        }
    
        public int getSalary()
        {
            return salary;
        }
    
        public void setSalary(int salary)
        {
            this.salary = salary;
        }
    
    }
    
    StreamDemo.java
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;
    
    /**
    List of objects -> List of String
    Get all the name values from a list of the SoftwareEngineer objects.
    */
    
    public class StreamDemo
    {
        public static void main(String[] args)
        {
    
            List<SoftwareEngineer> softwareEngineerList = Arrays.asList(
                    new SoftwareEngineer("Ram", 30, 10000),
                    new SoftwareEngineer("Peter", 27, 20000),
                    new SoftwareEngineer("Jai", 33, 30000));
    
            // Before Java 8
            List<String> nameList = new ArrayList<String>();
            for (SoftwareEngineer softwareEngineer : softwareEngineerList)
            {
                nameList.add(softwareEngineer.getName());
            }
            System.out.println(nameList);
    
            // Java 8
            List<String> collectNameList = softwareEngineerList.stream()
                                                    .map(softwareEngineer -> softwareEngineer.getName())
                                                    .collect(Collectors.toList());
            System.out.println(collectNameList);
    
        }
    
    }
    
    Output
    [Ram, Peter, Jai]
    [Ram, Peter, Jai]
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_name_swl_stream_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/53e39342c1c41555c6e548783b3cf96ab7bd9ee6/BasicJava/StreamDemo_name_swl_stream_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
  • How to convert lower alpha list to upper alpha list using Java 8 streams | Streams in Java 8


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

    NonStreamDemo.java
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class NonStreamDemo
    {
        public static void main(String[] args)
        {
    
            List<String> lowerAlphaList = Arrays.asList("a", "b", "c ");
            System.out.println("lowerAlphaList = "+lowerAlphaList); // [a, b, c, d]
    
            // Before Java8
            List<String> upperAlphaList = new ArrayList<>();
            for (String str : lowerAlphaList)
            {
                upperAlphaList.add(str.toUpperCase());
            }
    
            System.out.println("upperAlphaList = "+upperAlphaList); // [A, B, C, D]
    
        }
    
    }
    
    Output
    lowerAlphaList = [a, b, c ]
    upperAlphaList = [A, B, C ]
    
    StreamDemo1.java
    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;
    
    public class StreamDemo1
    {
        public static void main(String[] args)
        {
    
            List<String> lowerAlphaList = Arrays.asList("a", "b", "c ");
            System.out.println("lowerAlphaList = "+lowerAlphaList); // [a, b, c, d]
            // Java 8
            List<String> upperAlphaList = lowerAlphaList.stream().map(String::toUpperCase)
                                                                 .collect(Collectors.toList());
            System.out.println("upperAlphaList = "+upperAlphaList); // [A, B, C, D]     
            
        }
    
    }
    
    Output
    lowerAlphaList = [a, b, c ]
    upperAlphaList = [A, B, C ]
    
    StreamDemo2.java
    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;
    
    public class StreamDemo2
    {
        public static void main(String[] args)
        {
    
            // streams apply to any data type.
            List<Integer> numberList = Arrays.asList(1, 2, 3, 4, 5);
            
            List<Integer> resultList = numberList.stream().map(n -> n * 2)
                                                   .collect(Collectors.toList());
            
            System.out.println(resultList); // [2, 4, 6, 8, 10]
        }
    
    }
    
    Output
    [2, 4, 6, 8, 10]
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_LToU_list_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/53e39342c1c41555c6e548783b3cf96ab7bd9ee6/BasicJava/StreamDemo_LToU_list_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, 30 August 2017

    Kids: Men Fashion_V11

    Kids: Men Fashion_V10

    What is the difference between Stream vs Collection V3| Java 8 streams | Streams in Java 8


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

    Click the below Image to Enlarge
    What is the difference between Stream vs Collection V3| Java 8 streams | Streams in Java 8 
    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
  • Why is Java 8 Stream required? | Java 8 streams tutorial | Java 8 streams | Streams in Java 8


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

    Click the below Image to Enlarge
    Why is Java 8 Stream required? | Java 8 streams tutorial | Java 8 streams | Streams in Java 8 
    NonStreamDemo.java
    import java.util.Arrays;
    import java.util.Iterator;
    import java.util.List;
    
    /**
     * Before JDK8
     * 
     * We want to iterate over a list of integers and find out sum
     * of all the integers greater than 10.
     * 
     */
    public class NonStreamDemo
    {
        public static void main(String[] args)
        {
    
            List<Integer> numberList = Arrays.asList(2, 5, 10, 20, 50);
            int sum = calculateSum(numberList);
            System.out.println("sum = " + sum);
        }
    
        private static int calculateSum(List<Integer> list)
        {
            Iterator<Integer> it = list.iterator();
            int sum = 0;
            while (it.hasNext())
            {
                int num = it.next();
                if (num > 10)
                {
                    sum += num;
                }
            }
            return sum;
        }
    }
    
    Output
    sum = 70
    
    
    StreamDemo.java
    import java.util.Arrays;
    import java.util.List;
    
    /**
     * With JDK8
     * 
     * We want to iterate over a list of integers and find out sum
     * of all the integers greater than 10.
     * 
     */
    public class StreamDemo
    {
        public static void main(String[] args)
        {
    
            List<Integer> numberList = Arrays.asList(2, 5, 10, 20, 50);
            int sum = calculateSum(numberList);
            System.out.println("sum = " + sum);
        }
    
        private static int calculateSum(List<Integer> list)
        {
            return list.stream().filter(i -> i > 10).mapToInt(i -> i).sum();
        }
    }
    
    Output
    sum = 70
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_why_required_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/07d37f967453de17efa70af5825dc03ae2d5a7c0/BasicJava/StreamDemo_why_required_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
  • toArray method of Java 8 Stream | Java 8 streams tutorial | Java 8 streams | Streams in Java 8


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

    StreamToArrayDemo.java
    import java.util.Arrays;
    import java.util.List;
    
    public class StreamToArrayDemo
    {
        public static void main(String[] args)
        {
    
            List<String> list = Arrays.asList("A", "B", "C", "D");
    
            /*
             * toArray(): It returns an array containing the elements of
             * stream.
             */
    
            Object[] objArray = list.stream().toArray();
            System.out.println("Length of array: " + objArray.length);
    
            for (Object object : objArray)
            {
                System.out.println(object);
            }
        }
    }
    
    Output
    Length of array: 4
    A
    B
    C
    D
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_toArray_ObjArray_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/07d37f967453de17efa70af5825dc03ae2d5a7c0/BasicJava/StreamDemo_toArray_ObjArray_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
  • How to sort the map using sorted method of Java 8 Stream | Streams in Java 8


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

    StreamSortedDemo.java
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.Map;
    
    public class StreamSortedDemo
    {
        public static void main(String[] args)
        {
    
            Map<Integer, String> map = new HashMap<>();
            map.put(1, "Ball");
            map.put(2, "Cat");
            map.put(3, "Apple");
            map.put(4, "Dog");
    
            /*
             * sorted() : It returns a stream sorted with given
             * Comparator.
             */
            System.out.println("---Sort by Map Value---");
            map.entrySet().stream()
                    .sorted(Comparator.comparing(Map.Entry::getValue))
                    .forEach(e -> System.out.println("Key: " + e.getKey()
                            + ", Value: " + e.getValue()));
        }
    }
    
    Output
    ---Sort by Map Value---
    Key: 3, Value: Apple
    Key: 1, Value: Ball
    Key: 2, Value: Cat
    Key: 4, Value: Dog
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_Map_Sorted_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/07d37f967453de17efa70af5825dc03ae2d5a7c0/BasicJava/StreamDemo_Map_Sorted_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
  • Kids: Men Fashion_V8

    Kids: Men Fashion_V9

    Kids: Men Fashion_V6

    Kids: Men Fashion_V7