有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java流和筛选列表中包含的子列表中的数据

我有一本作者集和一本书集。书和作者之间的联系是作者的电子邮件地址

Book类有Set个作者的电子邮件地址:private Set<String> autoren

对于每本书,我想得到相应的作者,并打印出作者的姓和名

LinkedList<Author> autoren = (LinkedList<Autor>) dataController.getAutoren().stream()
        .filter(s -> buch.getAutoren().contains(s.getEmailadresse()));

for (Author author: autoren) {
    sysout(auther.getName())
}

我的布尔数据模型看起来像

public class Buch {

private String title;

private String isbnNummer;

private Set<String> autoren;}

如何获得所有书籍的所有作者的列表,并使用lambda表达式按书籍名称进行筛选


共 (1) 个答案

  1. # 1 楼答案

    你想要什么还不完全清楚,这里有一个可能有用的例子:

    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"))));
        }
    }