我通过ORM如下定义了三个用其getter和setter定义的类:
class Author {
Integer id;
String name;
}
class BookAuthor {
Integer id_book;
Author author;
}
class Book {
String title;
List<BookAuthor> authors;
}
我想从Book类创建一个id_author列表。我发现一种方法是使用流。我已经试过了:
List<Integer> result = authors.stream().map(BookAuthor::getAuthor::getId).collect(Collectors.toList());
但这似乎行不通。我可以访问Author类中的“ id”属性吗?
编辑:也许一种方法是:
List<Author> authorList = authors.stream().map(BookAuthor::getAuthor).collect(Collectors.toList());
List<Integer> result = authorList.stream().map(Author::getId).collect(Collectors.toList());
谢谢你。
我假设authors
变量是BookAuthor的列表(或集合),而不是Author(根据您的代码看起来像什么)。
我认为您有一个正确的主意,我只是认为您不能连锁::
运营商。
因此,请尝试使用lambda:
authors.stream().
map(ba -> ba.getAuthor().getId()).
collect(Collectors.toList());
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句