java - 流式传输和过滤列表中包含的子列表中的数据

标签 java lambda

我有一个作者集合LinkedList<Author> autoren ,以及一系列书籍List<Book> buecher 。一本书与其作者之间的联系是作者的电子邮件地址。

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())
}

我的 bool 数据模型看起来像

public class Buch {

private String title;

private String isbnNummer;

private Set<String> autoren;}

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

最佳答案

尚不完全清楚您想要什么,这里有一个可能有用的示例:

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

关于java - 流式传输和过滤列表中包含的子列表中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40276680/

相关文章:

java - 尝试为 Elasticsearch 创建映射

java - 目录为空时 NLST 上的 FTP 超时

c# - 为什么使用 `Delegate.CreateDelegate` 创建的委托(delegate)比 lambda 和方法委托(delegate)更快?

java - 线程停止问题android

java - R.java 文件在 Android 中未更新

java - 全屏 DialogFragment 与 StatusBar 重叠

c++ - 返回 lambda 表达式的函数

c++ - C++ 中的互斥量和 lambda 函数

lambda - Julia:如何使用需要超过 1 个参数的函数来过滤数组?

c# - 当 .NET 只依赖于参数时,我应该使用委托(delegate)还是方法?