The Artima Developer Community
Sponsored Link

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

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 unique names in upper case of the first 2 book authors that are 30 years old or older Posted: Jul 17, 2017 8:35 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 unique names in upper case of the first 2 book authors that are 30 years old or older
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=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
  • Read: How to get the unique names in upper case of the first 2 book authors that are 30 years old or older

    Topic: How to get the unique names in upper case of the first 2 book authors that are 30 years old or older Previous Topic   Next Topic Topic: Important notes of Java 8 Stream | Java 8 streams tutorial | Java 8 streams | Streams in Java 8

    Sponsored Links



    Google
      Web Artima.com   

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