I have three classes defined with their getters and setters as follows by a ORM:
class Author {
Integer id;
String name;
}
class BookAuthor {
Integer id_book;
Author author;
}
class Book {
String title;
List<BookAuthor> authors;
}
I'd like to create a list of id_author from the class Book. I've found that a way to do it is using streams. I've tried this:
List<Integer> result = authors.stream().map(BookAuthor::getAuthor::getId).collect(Collectors.toList());
But it does not seem to work. I can I access the "id" property in the Author class?
EDIT: maybe a way would be:
List<Author> authorList = authors.stream().map(BookAuthor::getAuthor).collect(Collectors.toList());
List<Integer> result = authorList.stream().map(Author::getId).collect(Collectors.toList());
Thank you.
I assume that the authors
variable is a list (or collection) of BookAuthor, not Author (that what it seems like based on your code).
I think you have the right idea, I just dont think you can chain ::
operators.
So try with lambda:
authors.stream().
map(ba -> ba.getAuthor().getId()).
collect(Collectors.toList());
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments