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
  • Important notes 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=ybO8BpkYFZU&list=UUhwKlOVR041tngjerWxVccw

    Click the below Image to Enlarge
    Important notes of Java 8 Stream | Java 8 streams tutorial | Java 8 streams | Streams in Java 8 
    Important notes of Java 8 Stream | Java 8 streams tutorial | 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 filter the productList based on price using Java 8 Stream | Streams in Java 8


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

    Click the below Image to Enlarge
    How to filter the productList based on price using Java 8 Stream | Streams in Java 8 
    Product.java
    class Product
    {
        private int id;
        private String name;
        private int price;
    
        public Product(int id, String name, int 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 int getPrice()
        {
            return price;
        }
    
        public void setPrice(int price)
        {
            this.price = price;
        }
    
        @Override
        public String toString()
        {
            return "Product [id=" + id + ", name=" + name + ", price="
                    + price + "]";
        }
    
    }
    
    StreamDemo.java
    import java.util.ArrayList;
    import java.util.List;
    import java.util.stream.Collectors;
    
    public class StreamDemo
    {
        public static void main(String[] args)
        {
            List<Product> productList = new ArrayList<Product>();
    
            // Adding Products
            productList.add(new Product(1, "Sony mobile", 25000));
            productList.add(new Product(2, "Lenovo mobile", 15000));
            productList.add(new Product(3, "Nokia mobile", 10000));
            productList.add(new Product(4, "Samsung mobile", 40000));
            productList.add(new Product(5, "Micromax mobile", 10000));
    
            List<Product> filteredProductList = productList.stream()
                    .filter(p -> p.getPrice() > 20000) // filtering data
                    .collect(Collectors.toList()); // collecting as list
            
    
            System.out.println(filteredProductList);
    
        }
    }
    
    Output
    [Product [id=1, name=Sony mobile, price=25000], Product [id=4, name=Samsung mobile, price=40000]]
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_filter_productList_price_20k_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/28b768ba4f37f4b22418a8a2d2a1f53c7d0de625/BasicJava/StreamDemo_filter_productList_price_20k_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 iterate method of Java 8 Stream | Java 8 streams tutorial | Streams in Java 8


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

    StreamDemo1.java
    import java.util.stream.Stream;
    
    /**
     * 
     * We can use stream to iterate any number of times. Stream provides
     * predefined methods to deal with the logic you implement.
     * 
     */
    public class StreamDemo1
    {
        public static void main(String[] args)
        {
            Stream.iterate(1, element -> element + 1)
                    .forEach(System.out::println);
        }
    }
    
    Output
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    ..
    ..
    ..
    ..
    
    StreamDemo2.java
    import java.util.stream.Stream;
    
    /**
     * 
     * We can use stream to iterate any number of times. Stream provides
     * predefined methods to deal with the logic you implement. In the
     * following example, we are iterating, filtering and passed a limit
     * to fix the iteration.
     * 
     */
    public class StreamDemo2
    {
        public static void main(String[] args)
        {
            Stream.iterate(1, element -> element + 1)
                    .filter(element -> element % 10 == 0).limit(5)
                    .forEach(System.out::println);
    
        }
    }
    
    Output
    10
    20
    30
    40
    50
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_iterate_method_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/28b768ba4f37f4b22418a8a2d2a1f53c7d0de625/BasicJava/StreamDemo_iterate_method_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 13 July 2017

    How to use method reference in Java 8 Stream | Java 8 streams tutorial | Streams in Java 8


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

    Product.java
    class Product
    {
        private int id;
        private String name;
        private int price;
    
        public Product(int id, String name, int 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 int getPrice()
        {
            return price;
        }
    
        public void setPrice(int price)
        {
            this.price = price;
        }
    
        @Override
        public String toString()
        {
            return "Product [id=" + id + ", name=" + name + ", price="
                    + price + "]";
        }
    
    }
    
    StreamDemo.java
    import java.util.ArrayList;
    import java.util.List;
    import java.util.stream.Collectors;
    
    /**
     * 
     * Method Reference in stream.
     * 
     */
    public class StreamDemo
    {
        public static void main(String[] args)
        {
            List<Product> productList = new ArrayList<Product>();
    
            // Adding Products
            productList.add(new Product(1, "Sony mobile", 25000));
            productList.add(new Product(2, "Lenovo mobile", 15000));
            productList.add(new Product(3, "Nokia mobile", 10000));
            productList.add(new Product(4, "Samsung mobile", 40000));
            productList.add(new Product(5, "Micromax mobile", 10000));
    
            List<String> productNameList = productList.stream()
                    .filter(p -> p.getPrice() > 20000) // filtering data
                    .map(Product::getName) // fetching Name by referring getName method
                    .collect(Collectors.toList()); // collecting as list
            
            System.out.println(productNameList);
    
        }
    }
    
    Output
    [Sony mobile, Samsung mobile]
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_method_ref_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/0279ebcce40946c6a80779824fa3df4a8c5201d9/BasicJava/StreamDemo_method_ref_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 the productList to map using Java 8 Stream | Java 8 streams tutorial | Java 8 streams


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

    Product.java
    class Product
    {
        private int id;
        private String name;
        private int price;
    
        public Product(int id, String name, int 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 int getPrice()
        {
            return price;
        }
    
        public void setPrice(int price)
        {
            this.price = price;
        }
    
        @Override
        public String toString()
        {
            return "Product [id=" + id + ", name=" + name + ", price="
                    + price + "]";
        }
    
    }
    
    StreamDemo.java
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;
    
    /**
     * 
     * Convert List into Map.
     * 
     */
    public class StreamDemo
    {
        public static void main(String[] args)
        {
            List<Product> productList = new ArrayList<Product>();
    
            // Adding Products
            productList.add(new Product(1, "Sony mobile", 25000));
            productList.add(new Product(2, "Lenovo mobile", 15000));
            productList.add(new Product(3, "Nokia mobile", 10000));
            productList.add(new Product(4, "Samsung mobile", 40000));
            productList.add(new Product(5, "Micromax mobile", 10000));
    
            // Converting Product List into a Map
            Map<String, Integer> productPriceMap = productList.stream()
                    .collect(Collectors.toMap(p -> p.getName(), p -> p.getPrice()));
    
            System.out.println(productPriceMap);
    
        }
    }
    
    Output
    {Nokia mobile=10000, Lenovo mobile=15000, Samsung mobile=40000, Micromax mobile=10000, Sony mobile=25000}
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_list_to_map_App.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/0279ebcce40946c6a80779824fa3df4a8c5201d9/BasicJava/StreamDemo_list_to_map_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