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

    Post a Comment