Thursday, 10 August 2017

How to convert Stream to Array using toArray method of Java 8 Stream | Streams in Java 8


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

StreamDemo.java
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 *
 * Convert Stream to array using stream.toArray(EntryType[]::new)
 *
 */
public class StreamDemo
{
    public static void main(String[] args)
    {
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 1; i < 20; i++)
        {
            list.add(i);
        }
        Stream<Integer> stream = list.stream();
        Integer[] evenNumbersArr = stream.filter(i -> i % 2 == 0)
                                         .toArray(Integer[]::new);

        for (Integer evenNumber : evenNumbersArr)
        {
            System.out.println(evenNumber);
        }
    }
}
Output
2
4
6
8
10
12
14
16
18

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/ed01c4e942c222c96b1953312e7a54d5a8556409/BasicJava/StreamDemo_StreamToArr_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 Stream to List using collect method of Java 8 Stream | Streams in Java 8


    Click here to watch in Youtube :
    https://www.youtube.com/watch?v=Fgprr6-LR7o&index=24&list=UUhwKlOVR041tngjerWxVccw

    StreamDemo.java
    import java.util.ArrayList;
    import java.util.List;
    import java.util.stream.Collectors;
    import 
    java.util.stream.Stream;
    
    /**
     * 
     * Convert Stream to List using stream.collect(Collectors.toList())
     *
     */
    public class StreamDemo
    {
        public static void main(String[] args)
        {
            List<Integer> list = new ArrayList<Integer>();
            for (int i = 1; i < 20; i++)
            {
                list.add(i);
            }
            Stream<Integer> stream = list.stream();
    
            List<Integer> evenNumbersList = stream.filter(i -> i % 2 == 0)
                                                  .collect(Collectors.toList());
    
            System.out.print(evenNumbersList);
        }
    }
    
    Output
    [2, 4, 6, 8, 10, 12, 14, 16, 18]
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_stream_to_list_collect_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/ed01c4e942c222c96b1953312e7a54d5a8556409/BasicJava/StreamDemo_stream_to_list_collect_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
  • What are the different ways we can create the streams | Java 8 streams | Streams in Java 8


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

    StreamDemo1.java
    import java.util.stream.Stream;
    
    /**
     * 
     * How to create stream Using Stream.of(val1, val2, val3….)
     */
    
    public class StreamDemo1
    {
        public static void main(String[] args)
        {
            Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
            stream.forEach(p -> System.out.println(p));
        }
    }
    
    Output
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    StreamDemo2.java
    import java.util.stream.Stream;
    
    /**
     * 
     * How to create stream Using Stream.of(arrayOfElements)
     */
    
    public class StreamDemo2
    {
        public static void main(String[] args)
        {
            Integer[] integerArray = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            Stream<Integer> stream = Stream.of(integerArray);
            stream.forEach(p -> System.out.println(p));
        }
    }
    
    Output
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    StreamDemo3.java
    import java.util.ArrayList;
    import java.util.List;
    import java.util.stream.Stream;
    
    /**
     * 
     * How to create stream Using someList.stream()
     */
    
    public class StreamDemo3
    {
        public static void main(String[] args)
        {
            List<Integer> list = new ArrayList<Integer>();
            for (int i = 1; i < 10; i++)
            {
                list.add(i);
            }
            Stream<Integer> stream = list.stream();
            stream.forEach(p -> System.out.println(p));
        }
    }
    
    Output
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    StreamDemo4.java
    import java.util.Date;
    import java.util.stream.Stream;
    
    /**
     * 
     * How to create stream Using Stream.generate() or Stream.iterate()
     * functions
     */
    
    public class StreamDemo4
    {
        public static void main(String[] args)
        {
            Stream<Date> stream = Stream.generate(() -> {return new Date(); });
            stream.forEach(p -> System.out.println(p));
        }
    }
    
    Output
    Sun Jul 16 09:18:50 IST 2017
    Sun Jul 16 09:18:50 IST 2017
    Sun Jul 16 09:18:50 IST 2017
    Sun Jul 16 09:18:50 IST 2017
    Sun Jul 16 09:18:50 IST 2017
    Sun Jul 16 09:18:50 IST 2017
    Sun Jul 16 09:18:50 IST 2017
    Sun Jul 16 09:18:50 IST 2017
    Sun Jul 16 09:18:50 IST 2017
    ---
    ---
    ---
    
    StreamDemo5.java
    import java.util.stream.IntStream;
    import java.util.stream.Stream;
    
    /**
     * 
     * How to create stream Using String chars or String tokens
     */
    
    public class StreamDemo5
    {
        public static void main(String[] args)
        {
            IntStream intStream = "12345_abcdefg".chars();
            intStream.forEach(p -> System.out.println(p));
    
            System.out.println("\n-------------------------\n");
    
            Stream<String> stream = Stream.of("A$B$C".split("\\$"));
            stream.forEach(p -> System.out.println(p));
        }
    }
    
    Output
    49
    50
    51
    52
    53
    95
    97
    98
    99
    100
    101
    102
    103
    
    -------------------------
    
    A
    B
    C
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_Create_diff_ways_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/ed01c4e942c222c96b1953312e7a54d5a8556409/BasicJava/StreamDemo_Create_diff_ways_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
  • What is the difference between Stream vs Collection| Java 8 streams | Streams in Java 8


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

    Click the below Image to Enlarge
    What is the difference between Stream vs Collection| 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
  • How to use Java 8 streams reduce method to concatenate the elements | Java 8 streams


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

    StreamDemo.java
    import java.util.Arrays;
    
    /**
     * If you want to perform more complex reduction operations, however,
     * you must use the reduce method.
     * 
     * The reduce method expects two arguments: an identity element, and a
     * lambda expression. You can think of the identity element as an
     * element which does not alter the result of the reduction operation. 
     * 
     */
    public class StreamDemo
    {
        public static void main(String[] args)
        {
            String[] strArray ={ "Welcome ", "to ", "India ", "Peter" };
    
            /*
             * Reduce method to concatenate all the elements.
             */
            String result = Arrays.stream(strArray).reduce("",(a, b) -> a + b);
    
            System.out.println(result);
        }
    }
    
    Output
    Welcome to India Peter
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_reduce_concatinate_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/ed01c4e942c222c96b1953312e7a54d5a8556409/BasicJava/StreamDemo_reduce_concatinate_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 use Java 8 streams reduction operations sum, count, and average| Streams in Java 8


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

    StreamDemo.java
    import java.util.Arrays;
    import java.util.OptionalDouble;
    
    /**
     * 
     * A reduction operation is one which allows you to compute a result
     * using all the elements present in a stream.
     * 
     * Reduction operations are also called terminal operations because
     * they are always present at the end of a chain of Stream methods
     * 
     * Java 8 includes several reduction methods, such as sum, average and
     * count, which allow to perform arithmetic operations on Stream
     * objects and get numbers as results.
     */
    public class StreamDemo
    {
        public static void main(String[] args)
        {
            int[] numberArray = { 2, 6, 8 };
            int sum = Arrays.stream(numberArray).sum();
            System.out.println("sum = " + sum);
    
            long count = Arrays.stream(numberArray).count();
            System.out.println("count = " + count);
    
            OptionalDouble optionalDouble = Arrays.stream(numberArray).average();
            System.out.println("average = " + optionalDouble.getAsDouble());
        }
    }
    
    Output
    sum = 16
    count = 3
    average = 5.333333333333333
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_reduction_sca_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/ed01c4e942c222c96b1953312e7a54d5a8556409/BasicJava/StreamDemo_reduction_sca_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, 26 July 2017

    How to filter the names whose length is greater than 4 using Java 8 streams | Streams in Java 8


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

    StreamDemo.java
    import java.util.Arrays;
    import java.util.stream.Stream;
    
    /**
     * If you have an array of strings, and you want to create a subset of
     * it which contains only those strings whose length is more than four
     * characters,then use below program.
     * 
     * How to filter the names whose length is greater than 4?
     */
    public class StreamDemo
    {
        public static void main(String[] args)
        {
            String[] nameArray = new String[] { "Peter", "Steve", "paul","Ram" };
    
            Stream<String> stream = Arrays.stream(nameArray);
    
            /*
             * The filter method expects a lambda expression as its
             * argument. However, the lambda expression passed to it must
             * always return a boolean value, which determines whether or
             * not the processed element should belong to the resulting
             * Stream object.
             */
            Stream<String> filteredStream = stream.filter(name -> name.length() > 4);
    
            String[] filteredNameArray = filteredStream.toArray(String[]::new);
    
            for (String name : filteredNameArray)
            {
                System.out.println(name);
            }
    
        }
    }
    
    Output
    Peter
    Steve
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_filter_name_gt_4_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/0b972e391c9826af8cfcb6980bab82db213fedd5/BasicJava/StreamDemo_filter_name_gt_4_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, 25 July 2017

    How to use map method of stream to convert all string elements in an array to uppercase


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

    StreamDemo.java
    import java.util.Arrays;
    import java.util.stream.Stream;
    
    /**
     * Once you have a Stream object, you can use a variety of methods to
     * transform it into another Stream object. The first such method
     * we’re going to look at is the map method. It takes a lambda
     * expression as its only argument, and uses it to change every
     * individual element in the stream. Its return value is a new Stream
     * object containing the changed elements.
     *
     * How to use map to convert all elements in an array of strings to
     * uppercase.
     */
    public class StreamDemo
    {
        public static void main(String[] args)
        {
            String[] nameArray = new String[] { "Peter", "Steve", "paul", "Ram" };
    
            Stream<String> stream = Arrays.stream(nameArray);
    
            /*
             * passing a lambda expression, one which can convert a string
             * to uppercase.
             *
             * The Stream object returned contains the changed strings.
             */
            Stream<String> upperCaseStream = stream.map(name -> name.toUpperCase());
    
            String[] upperCaseNameArray = upperCaseStream.toArray(String[]::new);
    
            for (String name : upperCaseNameArray)
            {
                System.out.println(name);
            }
    
        }
    }
    
    Output
    PETER
    STEVE
    PAUL
    RAM
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_map_uppercase_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/0b972e391c9826af8cfcb6980bab82db213fedd5/BasicJava/StreamDemo_map_uppercase_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, 19 July 2017

    How to convert List to stream and Array to Stream | Java 8 streams | Streams in Java 8


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

    Click the below Image to Enlarge
    How to convert List to stream and Array to Stream | Java 8 streams | Streams in Java 8 
    StreamDemo1.java
    import java.util.ArrayList;
    import java.util.List;
    import java.util.stream.Stream;
    
    
    public class StreamDemo1
    {
        public static void main(String[] args)
        {
            // Create an ArrayList
            List<Integer> numberList = new ArrayList<Integer>();
            numberList.add(1);
            numberList.add(5);
            numberList.add(8);
    
            // Convert it into a Stream
            Stream<Integer> stream = numberList.stream();
            System.out.println(stream);
            System.out.println("Count =  "+stream.count());
    
        }
    }
    
    Output
    java.util.stream.ReferencePipeline$Head@6d06d69c
    Count =  3
    
    StreamDemo2.java
    import java.util.Arrays;
    import java.util.stream.Stream;
    
    public class StreamDemo2
    {
        public static void main(String[] args)
        {
            // Create an array
            Integer[] numberArray ={ 1, 5, 8, 10 };
    
            // Convert it into a Stream
            Stream<Integer> stream = Arrays.stream(numberArray);
            System.out.println(stream);
            System.out.println("Count =  "+stream.count());
    
        }
    }
    
    Output
    java.util.stream.ReferencePipeline$Head@6d06d69c
    Count =  4
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_How_to_create_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/28b768ba4f37f4b22418a8a2d2a1f53c7d0de625/BasicJava/StreamDemo_How_to_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
  • Tuesday, 18 July 2017

    How to get the sum of ages of all male authors younger than 50 | Java 8 streams tutorial


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

    Author.java
    public class Author
    {
        private String name;
        private String email;
        private int age;
        private char gender;
    
        public Author(String name, String email, int age, char gender)
        {
            super();
            this.name = name;
            this.email = email;
            this.age = age;
            this.gender = gender;
        }
    
        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 int getAge()
        {
            return age;
        }
    
        public void setAge(int age)
        {
            this.age = age;
        }
    
        public char getGender()
        {
            return gender;
        }
    
        public void setGender(char gender)
        {
            this.gender = gender;
        }
    
    }
    
    Book.java
    public class Book
    {
    
        private String name;
        private Author author;
        private double price;
    
        public Book(String name, Author author, double price)
        {
            super();
            this.name = name;
            this.author = author;
            this.price = price;
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName(String name)
        {
            this.name = name;
        }
    
        public Author getAuthor()
        {
            return author;
        }
    
        public void setAuthor(Author author)
        {
            this.author = author;
        }
    
        public double getPrice()
        {
            return price;
        }
    
        public void setPrice(double price)
        {
            this.price = price;
        }
    
    }
    
    StreamDemo.java
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     *
     * How to get the sum of ages of all male authors younger than 50.
     *
     */
    public class StreamDemo
    {
        public static void main(String[] args)
        {
            List<Book> bookList = new ArrayList<Book>();
    
            // Adding Books
            bookList.add(new Book("Java Basics",
                    new Author("Peter", "peter@yahoo.com", 20, 'M'),1000.50));
    
            bookList.add(new Book("Mysql Basics",
                    new Author("Steve", "steve@yahoo.com", 30, 'M'),2000.0));
    
            bookList.add(new Book("Oracle Basics",
                    new Author("John", "john@yahoo.com", 40, 'M'),3000.0));
    
            bookList.add(new Book("Angular Basics",
                    new Author("Juli", "juli@yahoo.com", 55, 'F'),3000.0));
    
            bookList.add(new Book("Jquery Basics",
                    new Author("Dave", "dave@yahoo.com", 65, 'M'),1000.0));
    
            /*
             * Using the same original stream we once again map the
             * elements from Books to Authors and filter just on those
             * authors that are male. Next we map the elements from
             * Authors to author ages which gives us a stream of ints. We
             * filter ages to just those that are less than 50 and use a
             * reduce operation and Integer::sum to total the ages.
             */
    
            Integer sumOfMaleAuthorAge = 
                     bookList.stream()//Stream<Book>
                    .map(Book::getAuthor) //Stream<Book> to Stream<Author>.
                    .filter(author -> author.getGender() == 'M') // filter the male Authors.
                    .map(Author::getAge)//Stream<Author> to Stream<age>.
                    .filter(age -> age < 50)//filter the age<50.
                    .reduce(0, Integer::sum);// calculate the sum of ages.
    
            System.out.println("sumOfMaleAuthorAge = " + sumOfMaleAuthorAge);
    
        }
    }
    
    Output
    sumOfMaleAuthorAge = 90
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_Book_Auther_sum_age_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/28b768ba4f37f4b22418a8a2d2a1f53c7d0de625/BasicJava/StreamDemo_Book_Auther_sum_age_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
  • Monday, 17 July 2017

    How to get the unique names in upper case of the first 2 book authors that are 30 years old or older


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

    Author.java
    public class Author
    {
        private String name;
        private String email;
        private int age;
    
        public Author(String name, String email, int age)
        {
            super();
            this.name = name;
            this.email = email;
            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 int getAge()
        {
            return age;
        }
    
        public void setAge(int age)
        {
            this.age = age;
        }
    
    }
    
    Book.java
    public class Book
    {
    
        private String name;
        private Author author;
        private double price;
    
        public Book(String name, Author author, double price)
        {
            super();
            this.name = name;
            this.author = author;
            this.price = price;
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName(String name)
        {
            this.name = name;
        }
    
        public Author getAuthor()
        {
            return author;
        }
    
        public void setAuthor(Author author)
        {
            this.author = author;
        }
    
        public double getPrice()
        {
            return price;
        }
    
        public void setPrice(double price)
        {
            this.price = price;
        }
    
    }
    
    StreamDemo.java
    import java.util.ArrayList;
    import java.util.List;
    import java.util.stream.Collectors;
    
    /**
     *
     * Get the unique names in uppercase of the first 2 book authors
     * that are 30 years old or older.
     *
     */
    public class StreamDemo
    {
        public static void main(String[] args)
        {
            List<Book> bookList = new ArrayList<Book>();
    
            // Adding Books
            bookList.add(new Book("Java Basics",
                    new Author("Peter", "peter@yahoo.com", 25), 1000.50));
    
            bookList.add(new Book("Mysql Basics",
                    new Author("Steve", "steve@yahoo.com", 35), 2000.0));
    
            bookList.add(new Book("Oracle Basics",
                    new Author("John", "john@yahoo.com", 45), 3000.0));
    
            bookList.add(new Book("Angular Basics",
                    new Author("Dave", "dave@yahoo.com", 55), 3000.0));
    
            bookList.add(new Book("Jquery Basics",
                    new Author("Dave", "dave@yahoo.com", 55), 1000.0));
    
    
            List<String> filteredAutherNameList = new ArrayList<String>();
    
            /*
             * From this list of books, we first need to map from books to
             * the book authors which gets us a stream of Authors and then
             * filter them to just get those authors that are 30 or over.
             * We’ll map the name of the Author, which returns us a
             * stream of Strings. We’ll map this to uppercase Strings and
             * make sure the elements are unique in the stream and grab
             * the first 2. Finally we return this as a list using toList
             * from java.util.streams.Collectors.
             */
    
            filteredAutherNameList = bookList.stream() // Stream of book
                    .map(book -> book.getAuthor()) // Stream<book> to Stream<Author>
                    .filter(author -> author.getAge() >= 30) // Filter the author whose Age is >=30
                    .map(Author::getName) //Stream<Author> to Stream<Name>
                    .map(String::toUpperCase) // Convert name as upper case
                    .distinct() // Get the unique elements[i.e. name]
                    .limit(2) // Grab the first 2
                    .collect(Collectors.toList()); // collect the result as a list
    
            System.out.println(filteredAutherNameList);
    
        }
    }
    
    Output
    [STEVE, JOHN]
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_Book_Auther_name_App.zip?attredirects=0&d=1

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

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

    Google+ Badge