Friday, 20 October 2017

What are the different Stream operations supported by Java 8 Stream | Streams in Java 8


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

Click the below Image to Enlarge
What are the different Stream operations supported by Java 8 Stream | 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
  • How to convert streams to other data sources | Streams in Java 8


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

    StreamDemo1.java
    import java.util.List;
    import java.util.stream.Collectors;
    import java.util.stream.Stream;
    
    /**
     * Convert Streams to Other DataStructures
     * 
     * Get Collections using stream.collect(Collectors.toList()).
     */
    public class StreamDemo1
    {
        public static void main(String[] args)
        {
            Stream<String> stream = Stream.of("Peter", "John", "Ram");
            
            List<String> nameList = stream.collect(Collectors.toList());
    
            System.out.println(nameList);
        }
    
    }
    
    Output
    [Peter, John, Ram]
    
    
    StreamDemo2.java
    import java.util.ArrayList;
    import java.util.List;
    import java.util.stream.Stream;
    
    /**
     * Convert Streams to Other DataStructures
     * 
     * Get arrays using stream.toArray(EntryType[]::new)
     */
    public class StreamDemo2
    {
        public static void main(String[] args)
        {
            List<Integer> numberList = new ArrayList<>();
            numberList.add(1);
            numberList.add(2);
            numberList.add(3);
            numberList.add(4);
            
            Stream<Integer> stream = numberList.stream();
            Integer[] evenArray = stream.filter(i -> i % 2 == 0).toArray(Integer[]::new);
            for (Integer integer : evenArray)
            {
                System.out.println(integer);
            }
        }
    
    }
    
    Output
    2
    4
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_convert_list_array.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/a9fa8aa6476993bce8a79f4d0e8f1799d1d35a7f/BasicJava/StreamDemo_convert_list_array/?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 generate stream using generate and iterate methods of Java 8 streams | Streams in Java 8


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

    StreamDemo1.java
    import java.util.Arrays;
    import java.util.stream.Stream;
    /**
     * Generate Stream using Stream.generate()
     */
    public class StreamDemo1
    {
        public static void main(String[] args)
        {
            Stream<String> stream = Stream.generate(() -> "welcome").limit(5);
            String[] strArr = stream.toArray(String[]::new);
            System.out.println(Arrays.toString(strArr));
        }
    
    }
    
    Output
    [welcome, welcome, welcome, welcome, welcome]
    
    
    StreamDemo2.java
    import java.util.stream.Stream;
    /**
     * Generate Stream using Stream.iterate().
     */
    public class StreamDemo2
    {
        public static void main(String[] args)
        {
            Stream<Long> iterateNumbers = Stream.iterate(1L, n -> n + 10).limit(5);
            iterateNumbers.forEach(System.out::println);
        }
    
    }
    
    Output
    1
    11
    21
    31
    41
    
    
    StreamDemo3.java
    import java.util.regex.Pattern;
    /**
     * Generate Stream from APIs like Regex.
     */
    public class StreamDemo3
    {
        public static void main(String[] args)
        {
            String str = "Welcome,to,India";
            Pattern.compile(",").splitAsStream(str).forEach(System.out::println);
        }
    
    }
    
    Output
    Welcome
    to
    India
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_generate_iterate_regex.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/a9fa8aa6476993bce8a79f4d0e8f1799d1d35a7f/BasicJava/StreamDemo_generate_iterate_regex/?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, 17 October 2017

    How to generate stream from arrays and collections | Java 8 streams | Streams in Java 8


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

    StreamDemo1.java
    import java.util.Arrays;
    import java.util.stream.Stream;
    /**
     * Generate Streams from Arrays using .stream or Stream.of.
     */
    public class StreamDemo1
    {
        public static void main(String[] args)
        {
            String[] strArray = { "Apple", "Ball", "Cat", "Dog", "Eagle" };
    
            // Get Stream using the Arrays.stream
            Stream<String> stream1 = Arrays.stream(strArray);
            stream1.forEach(x -> System.out.println(x));
    
            System.out.println("---------------------------------");
            
            // Get Stream using the Stream.of
            Stream<String> stream2 = Stream.of(strArray);
            stream2.forEach(x -> System.out.println(x));
        }
    
    }
    
    Output
    Apple
    Ball
    Cat
    Dog
    Eagle
    ---------------------------------
    Apple
    Ball
    Cat
    Dog
    Eagle
    
    
    StreamDemo2.java
    import java.util.ArrayList;
    import java.util.List;
    /**
     * Generate Streams from Collections.
     */
    public class StreamDemo2
    {
        public static void main(String[] args)
        {
            List<String> itemList = new ArrayList<>();
            itemList.add("Apple");
            itemList.add("Ball");
            itemList.add("Cat");
            itemList.add("Dog");
    
            itemList.stream().forEach(item -> System.out.println(item));
        }
    
    }
    
    Output
    Apple
    Ball
    Cat
    Dog
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_generate_array_collection.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/a9fa8aa6476993bce8a79f4d0e8f1799d1d35a7f/BasicJava/StreamDemo_generate_array_collection/?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
  • What is java Stream | Java 8 streams tutorial | Java 8 streams | Streams in Java 8


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

    Click the below Image to Enlarge
    What is java Stream | Java 8 streams tutorial | Java 8 streams | Streams in Java 8
    What is java Stream | Java 8 streams tutorial | Java 8 streams | Streams in Java 8

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

    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: Baa baa black sheep | baa baa black sheep nursery rhyme

    Kids: Rain Rain go away | rain rain go away little johnny wants to play

    How to accumulate the names of all persons using reduce method of Java 8 Stream | Streams in Java 8


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

    Person.java
    enum Gender
    {
        MALE, FEMALE
    }
    
    public class Person
    {
        private String name;
        private String email;
        private Gender gender;
        private int age;
    
        public Person(String name, String email, Gender gender, int age)
        {
            super();
            this.name = name;
            this.email = email;
            this.gender = gender;
            this.age = age;
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName(String name)
        {
            this.name = name;
        }
    
        public String getEmail()
        {
            return email;
        }
    
        public void setEmail(String email)
        {
            this.email = email;
        }
    
        public Gender getGender()
        {
            return gender;
        }
    
        public void setGender(Gender gender)
        {
            this.gender = gender;
        }
    
        public int getAge()
        {
            return age;
        }
    
        public void setAge(int age)
        {
            this.age = age;
        }
    
        @Override
        public String toString()
        {
            return "Person [name=" + name + "]";
        }
    
    }
    
    StreamReduceDemo1.java
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Optional;
    
    public class StreamReduceDemo1
    {
        public static void main(String[] args)
        {
            List<Person> personList = new ArrayList<>();
    
            personList.add(new Person("Alice","alice@gmail.com",    Gender.FEMALE, 26));
            personList.add(new Person("Bob","bob@gmail.com", Gender.MALE, 32));
            personList.add(new Person("Carol","carol@gmail.com",Gender.FEMALE, 23));
            personList.add(new Person("David","david@gmail.com",    Gender.MALE, 39));
            personList.add(new Person("Eric","eric@gmail.com", Gender.MALE, 26));
            
            /*
             * 
             * Version #1: Optional<T> reduce(BinaryOperator<T> accumulator) - This
             * method performs a reduction on the elements of the stream, using an
             * associative accumulation function, and returns an Optional object
             * describing the reduced value.
             * 
             * For example, the following code accumulates names of all persons into
             * a String:
             */
            Optional<String> optional = personList.stream().map(p -> p.getName())
                    .reduce((name1, name2) -> name1 + ", " + name2);
    
            if (optional.isPresent())
            {
                String names = optional.get();
                System.out.println(names);
            }
    
        }
    
    }
    
    Output
    Alice, Bob, Carol, David, Eric
    
    
    StreamReduceDemo2.java
    import java.util.Arrays;
    
    public class StreamReduceDemo2
    {
        public static void main(String[] args)
        {
            int[] numberArray = { 2, 3, 5};
    
            /*
             * Version #2: T reduce(T identity, BinaryOperator<T> accumulator) -
             * This method is similar to the version #1, but it returns the reduced
             * value of the specified type T. The identity value must be an identity
             * value for the accumulator function, which means it does not affect
             * the result of accumulation.
             * 
             * The following code calculates sum of numbers in a stream:
             */
            int sum = Arrays.stream(numberArray).reduce(0, (x, y) -> (x + y));
    
            System.out.println("sum = " + sum);
    
        }
    
    }
    
    Output
    sum = 10
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_reduce_accumulate.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/a9fa8aa6476993bce8a79f4d0e8f1799d1d35a7f/BasicJava/StreamDemo_reduce_accumulate/?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
  • Monday, 16 October 2017

    How to find the oldest male person using max method of Java 8 Stream | Streams in Java 8


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

    Person.java
    enum Gender
    {
        MALE, FEMALE
    }
    
    public class Person
    {
        private String name;
        private String email;
        private Gender gender;
        private int age;
    
        public Person(String name, String email, Gender gender, int age)
        {
            super();
            this.name = name;
            this.email = email;
            this.gender = gender;
            this.age = age;
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName(String name)
        {
            this.name = name;
        }
    
        public String getEmail()
        {
            return email;
        }
    
        public void setEmail(String email)
        {
            this.email = email;
        }
    
        public Gender getGender()
        {
            return gender;
        }
    
        public void setGender(Gender gender)
        {
            this.gender = gender;
        }
    
        public int getAge()
        {
            return age;
        }
    
        public void setAge(int age)
        {
            this.age = age;
        }
    
        @Override
        public String toString()
        {
            return "Person [name=" + name + "]";
        }
    
    }
    
    StreamMaxDemo.java
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Optional;
    
    public class StreamMaxDemo
    {
        public static void main(String[] args)
        {
            List<Person> personList = new ArrayList<>();
    
            personList.add(new Person("Alice","alice@gmail.com",    Gender.FEMALE, 26));
            personList.add(new Person("Bob","bob@gmail.com", Gender.MALE, 32));
            personList.add(new Person("Carol","carol@gmail.com",Gender.FEMALE, 23));
            personList.add(new Person("David","david@gmail.com",    Gender.MALE, 39));
            personList.add(new Person("Eric","eric@gmail.com", Gender.MALE, 26));
            
            /*
             * Similar to the min() operation, the max() is a special reduction
             * operation that returns the maximum element in the stream according to
             * the specified comparator.
             * 
             * The following code finds the oldest male person in the list:
             */
            Optional<Person> optional = personList.stream()
                    .filter(p -> p.getGender().equals(Gender.MALE))
                    .max((p1, p2) -> p1.getAge() - p2.getAge());
            if (optional.isPresent())
            {
                Person oldestMan = optional.get();
                System.out.println("The oldest man is: " + oldestMan.getName()
                        + " (" + oldestMan.getAge() + ")");
            }
    
        }
    
    }
    
    Output
    The oldest man is: David (39)
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_max_oldestman.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/17906082b9d83230789adc7b5df551c73ce2b46f/BasicJava/StreamDemo_max_oldestman/?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: Humpty Dumpty | Humpty Dumpty sat on a wall

    Kids: Johny Johny yes papa | Johny Johny yes papa rhymes

    Thursday, 12 October 2017

    Kids: Jack and Jill went up the hill | Jack and Jill nursery rhyme

    How to find the youngest female person using min method of Java 8 Stream | Streams in Java 8


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

    Person.java
    enum Gender
    {
        MALE, FEMALE
    }
    
    public class Person
    {
        private String name;
        private String email;
        private Gender gender;
        private int age;
    
        public Person(String name, String email, Gender gender, int age)
        {
            super();
            this.name = name;
            this.email = email;
            this.gender = gender;
            this.age = age;
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName(String name)
        {
            this.name = name;
        }
    
        public String getEmail()
        {
            return email;
        }
    
        public void setEmail(String email)
        {
            this.email = email;
        }
    
        public Gender getGender()
        {
            return gender;
        }
    
        public void setGender(Gender gender)
        {
            this.gender = gender;
        }
    
        public int getAge()
        {
            return age;
        }
    
        public void setAge(int age)
        {
            this.age = age;
        }
    
        @Override
        public String toString()
        {
            return "Person [name=" + name + "]";
        }
    
    }
    
    StreamMinDemo.java
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Optional;
    
    public class StreamMinDemo
    {
        public static void main(String[] args)
        {
            List<Person> personList = new ArrayList<>();
    
            personList.add(new Person("Alice","alice@gmail.com",    Gender.FEMALE, 26));
            personList.add(new Person("Bob","bob@gmail.com", Gender.MALE, 32));
            personList.add(new Person("Carol","carol@gmail.com",Gender.FEMALE, 23));
            personList.add(new Person("David","david@gmail.com",    Gender.MALE, 39));
            personList.add(new Person("Eric","eric@gmail.com", Gender.MALE, 26));
            
            /*
             * The min(comparator) is a special reduction operation that returns the
             * minimum element in the stream according to the provided comparator.
             * It returns an Optional which is a container object that contains the
             * value. 
             * 
             * For example, the following code snippet finds the youngest
             * female person in the list:
             */
            Optional<Person> optional = personList.stream()
                    .filter(p -> p.getGender().equals(Gender.FEMALE))
                    .min((p1, p2) -> p1.getAge() - p2.getAge());
            
            if (optional.isPresent())
            {
                Person youngestGirl = optional.get();
                System.out.println("The youngest girl is: " + youngestGirl.getName()
                        + " (" + youngestGirl.getAge() + ")");
            }
            
        }
    
    }
    
    Output
    The youngest girl is: Carol (23)
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_min_youngestgirl.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/17906082b9d83230789adc7b5df551c73ce2b46f/BasicJava/StreamDemo_min_youngestgirl/?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 use the forEach method of Java 8 Stream | Streams in Java 8


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

    Person.java
    enum Gender
    {
        MALE, FEMALE
    }
    
    public class Person
    {
        private String name;
        private String email;
        private Gender gender;
        private int age;
    
        public Person(String name, String email, Gender gender, int age)
        {
            super();
            this.name = name;
            this.email = email;
            this.gender = gender;
            this.age = age;
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName(String name)
        {
            this.name = name;
        }
    
        public String getEmail()
        {
            return email;
        }
    
        public void setEmail(String email)
        {
            this.email = email;
        }
    
        public Gender getGender()
        {
            return gender;
        }
    
        public void setGender(Gender gender)
        {
            this.gender = gender;
        }
    
        public int getAge()
        {
            return age;
        }
    
        public void setAge(int age)
        {
            this.age = age;
        }
    
        @Override
        public String toString()
        {
            return "Person [name=" + name + "]";
        }
    
    }
    
    StreamForEachDemo.java
    import java.util.ArrayList;
    import java.util.List;
    
    public class StreamForEachDemo
    {
        public static void main(String[] args)
        {
            List<Person> personList = new ArrayList<>();
    
            personList.add(new Person("Alice","alice@gmail.com",    Gender.FEMALE, 26));
            personList.add(new Person("Bob","bob@gmail.com", Gender.MALE, 32));
            personList.add(new Person("Carol","carol@gmail.com",Gender.FEMALE, 23));
            personList.add(new Person("David","david@gmail.com",    Gender.MALE, 39));
            personList.add(new Person("Eric","eric@gmail.com", Gender.MALE, 26));
    
            /*
             * The forEach() operation performs an action for each element in the
             * stream, thus creating a side effect, such as print out information of
             * each female person as shown in the following code:
             */
            personList.stream().filter(p -> p.getGender().equals(Gender.FEMALE))
                                                        .forEach(System.out::println);
    
        }
    
    }
    
    Output
    Person [name=Alice]
    Person [name=Carol]
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_foreach_female_operation.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/17906082b9d83230789adc7b5df551c73ce2b46f/BasicJava/StreamDemo_foreach_female_operation/?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