The Artima Developer Community
Sponsored Link

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

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
How to get the sum of ages of all male authors younger than 50 | Java 8 streams tutorial Posted: Jul 18, 2017 6:36 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: How to get the sum of ages of all male authors younger than 50 | Java 8 streams tutorial
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

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
  • Read: How to get the sum of ages of all male authors younger than 50 | Java 8 streams tutorial

    Topic: How to get the sum of ages of all male authors younger than 50 | Java 8 streams tutorial Previous Topic   Next Topic Topic: Outputting the given, when, then, Extending Spock

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use