I have a collection of authors LinkedList<Author> autoren
, and a collection of books List<Book> buecher
. The connection between a book and its author is the email address of the author.
我有一组作者LinkedList
The Book class has a Set
of authors' email addresses: private Set<String> autoren
.
Book类有一组作者的电子邮件地址:private Set
For each book, I want to get the corresponding author-authors and print the last name and first name of the author.
对于每本书,我想获得相应的作者 - 作者并打印作者的姓氏和名字。
LinkedList<Author> autoren = (LinkedList<Autor>) dataController.getAutoren().stream()
.filter(s -> buch.getAutoren().contains(s.getEmailadresse()));
for (Author author: autoren) {
sysout(auther.getName())
}
my data model for bool looks like
我的bool数据模型看起来像
public class Buch {
private String title;
private String isbnNummer;
private Set<String> autoren;}
How can I get a list of all authors of all books, and filter by book name, using a lambda expression?
如何获取所有书籍的所有作者的列表,并使用lambda表达式按书名过滤?
1 个解决方案
#1
1
It is not entirely clear what you want, here an example that might be helpful:
目前还不完全清楚你想要什么,这里有一个可能有用的例子:
package ch.revault.java8;
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
public class AppTest {
@Test
public void testApp() {
List<Book> books = getBooks();
List<Author> authors = getAuthors();
String yourBookNameFilter = "The martian";
Map<String, Author> authorsByEmail = authors
.stream()
.collect(toMap(a -> a.email, a -> a));
books.stream()
.filter(b -> b.title.contains(yourBookNameFilter)) // <-- simple
// filter,
.flatMap(b -> b.authorEmails.stream())
.distinct()
.map(e -> authorsByEmail.get(e)) // you could inline
// authorsByEmail lookup
.forEach(a -> System.out.println(format("%s, %s", a.firstName, a.lastName)));
}
public class Author {
final String firstName;
final String lastName;
final String email;
public Author(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
}
public class Book {
final String title;
final String isbnNummer;
final Set<String> authorEmails;
public Book(String title, String isbnNummer, Set<String> authorEmails) {
this.title = title;
this.isbnNummer = isbnNummer;
this.authorEmails = authorEmails;
}
}
private List<Author> getAuthors() {
return asList(
new Author("f1", "l1", "e1@example.com"),
new Author("f2", "l2", "e2@example.com"),
new Author("f3", "l3", "e3@example.com"),
new Author("f4", "l4", "e4@example.com"));
}
private List<Book> getBooks() {
return asList(
new Book("The martian", "i1", new LinkedHashSet<>(asList("e2@example.com", "e4@example.com"))),
new Book("t2", "i2",
new LinkedHashSet<>(asList("e1@example.com", "e2@example.com", "e3@example.com"))));
}
}
#1
1
It is not entirely clear what you want, here an example that might be helpful:
目前还不完全清楚你想要什么,这里有一个可能有用的例子:
package ch.revault.java8;
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
public class AppTest {
@Test
public void testApp() {
List<Book> books = getBooks();
List<Author> authors = getAuthors();
String yourBookNameFilter = "The martian";
Map<String, Author> authorsByEmail = authors
.stream()
.collect(toMap(a -> a.email, a -> a));
books.stream()
.filter(b -> b.title.contains(yourBookNameFilter)) // <-- simple
// filter,
.flatMap(b -> b.authorEmails.stream())
.distinct()
.map(e -> authorsByEmail.get(e)) // you could inline
// authorsByEmail lookup
.forEach(a -> System.out.println(format("%s, %s", a.firstName, a.lastName)));
}
public class Author {
final String firstName;
final String lastName;
final String email;
public Author(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
}
public class Book {
final String title;
final String isbnNummer;
final Set<String> authorEmails;
public Book(String title, String isbnNummer, Set<String> authorEmails) {
this.title = title;
this.isbnNummer = isbnNummer;
this.authorEmails = authorEmails;
}
}
private List<Author> getAuthors() {
return asList(
new Author("f1", "l1", "e1@example.com"),
new Author("f2", "l2", "e2@example.com"),
new Author("f3", "l3", "e3@example.com"),
new Author("f4", "l4", "e4@example.com"));
}
private List<Book> getBooks() {
return asList(
new Book("The martian", "i1", new LinkedHashSet<>(asList("e2@example.com", "e4@example.com"))),
new Book("t2", "i2",
new LinkedHashSet<>(asList("e1@example.com", "e2@example.com", "e3@example.com"))));
}
}