Question

In java Question: Using Streams to Get the unique surnames in uppercase of the first 10...

In java

Question: Using Streams to Get the unique surnames in uppercase of the first 10 book authors that are 50 years old or older.

Assume that a variable library is defined as an ArrayList. Each Element in the arraylist, representing a book, is an object from a Book class. The Book class has the book's name (a string), book ISBN number (a string), and the author as data fields (an object from the Author class). The Book class has a method getAuthor to return the author of the book.

The Author class holds the author's first name. last name, age as data fields. The Author class also has methods to return the data fields (getFirstName to return the first name, getLastName to return the last name, and getAge to return the age).

Requirements:

1. define the Author class and Book class

2. In the demo program, create the library as an ArrayList and add 20 books to the arraylist

3. In the demo program, using stream to get the unique surnames in uppercase of the first 10 book authors that are 50 years old or older.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

JAVA PROGRAM:

---------Book.java-------------

public class Book {

public String book_name;

public String book_isbn;

public Author author;

public Book() {

this.book_name = "";

this.book_isbn = "";

this.author = new Author();

}

public Author getAuthor(){

return author;

}

public void setBookName(String bnme){

this.book_name = bnme;

}

public void setBookISBN(String bisbn){

this.book_isbn = bisbn;

}

public String getBookName(){

return book_name;

}

public String getBookISBN(){

return book_isbn;

}

}

----------Author.java---------------

public class Author {

public int auth_age;

public String auth_fname;

public String auth_lname;

public Author() {

this.auth_age = 0;

this.auth_fname = "";

this.auth_lname = "";

}

public int getAge(){

return auth_age;

}

public String getFname(){

return auth_fname;

}

public String getLname(){

return auth_lname;

}

public void setAge(int age){

this.auth_age = age;

}

public void setFname(String fname){

this.auth_fname = fname;

}

public void setLname(String lname){

this.auth_lname = lname;

}

}

----------BookAuthorMain.java------------

import java.util.List;

import java.util.stream.Collectors;

import java.util.function.Function;

import java.util.function.Predicate;

import java.util.Map;

import java.util.concurrent.ConcurrentHashMap;

import java.util.*;

public class BookAuthorMain {

public static void main(String args[]) {

ArrayList<Book> library = new ArrayList<Book>();

Book bookDetails;

String ch="", bnme="", bisbn="", afnme="", alnme="";

int aage, i=0;

boolean q = false;

Scanner in=new Scanner(System.in);


do{

System.out.println("\n--------Enter book details-------- ");

bookDetails = new Book();

System.out.println("Enter book name: ");

bnme = in.nextLine();

System.out.println("Enter book ISBN number: ");

bisbn = in.nextLine();

System.out.println("Enter book author first name: ");

afnme = in.nextLine();

System.out.println("Enter book author last name: ");

alnme = in.nextLine();

System.out.println("Enter book author age: ");

aage = in.nextInt();


bookDetails.setBookName(bnme);

bookDetails.setBookISBN(bisbn);

bookDetails.author.setFname(afnme);

bookDetails.author.setLname(alnme);

bookDetails.author.setAge(aage);

library.add(bookDetails);

System.out.println("Data addeed for " + library.get(i).book_name);

System.out.println(library.size());

i++;

ch = in.nextLine();

System.out.print("\nDo you want to continue?(y/n): ");

ch = in.nextLine();

if((ch.equals("n")) || ((ch.equals("N")))){

q = true;

}

}while(!q);


List<Book> ageResult = library.stream()

.filter(lib -> lib.author.auth_age >= 50)

.collect(Collectors.toList());

List<Book> distinctElements = ageResult.stream()

.filter(distinctByKey(p -> p.author.getLname()))

.collect(Collectors.toList());

if(distinctElements.size()>10){

for(int j=0; j < 10; j++){

System.out.println("Author Last name: " + distinctElements.get(j).author.getLname().toUpperCase() +

"\tAge: " + distinctElements.get(j).author.getAge());

}

} else{

for(int j=0; j < distinctElements.size(); j++){

System.out.println("Author Last name: " + distinctElements.get(j).author.getLname().toUpperCase() +

"\tAge: " + distinctElements.get(j).author.getAge());

}

}

}


public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor){

Map<Object, Boolean> map = new ConcurrentHashMap<>();

return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;

}

}

Add a comment
Know the answer?
Add Answer to:
In java Question: Using Streams to Get the unique surnames in uppercase of the first 10...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT