1. There isn’t a field with name "author" in the class Book.
2.
public class TestClass {
public static void main(String[] args) {
List<Book> books = getBooksByAuthor("Ludlum");
books.stream().sorted().forEach(b -> System.out.println(b.getIsbn()));
}
private static List<Book> getBooksByAuthor(String ludlum) {
List<Book> al = new ArrayList<>();
al.add(new Book("123", "Big Bang"));
return al;
}
}
It will no throw a ClassCastException and print "123".
About Question enthuware.ocpjp.v8.2.1875 :
Moderator: admin
-
- Posts: 7
- Joined: Fri Dec 11, 2015 6:50 am
- Contact:
-
- Site Admin
- Posts: 10075
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1875 :
That is ok. The problem statement asks you to assume that getBooksByAuthor is a valid method that returns a List of Books. This method might return the list of books through some other means (not necessarily by looking at a field of the class).surzhin wrote:1. There isn’t a field with name "author" in the class Book.
getBooksByAuthor returns a List of Books (plural). So it could contain more than one book.2.
public class TestClass {
public static void main(String[] args) {
List<Book> books = getBooksByAuthor("Ludlum");
books.stream().sorted().forEach(b -> System.out.println(b.getIsbn()));
}
private static List<Book> getBooksByAuthor(String ludlum) {
List<Book> al = new ArrayList<>();
al.add(new Book("123", "Big Bang"));
return al;
}
}
It will no throw a ClassCastException and print "123".
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 45 guests