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
  • No comments:

    Post a Comment