FarFarAway
FarFarAway

Reputation: 1115

stream and filter data in a sublist contained in a list

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.

The Book class has a Set of authors' email addresses: private Set<String> autoren.

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

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?

Upvotes: 0

Views: 4962

Answers (1)

revau.lt
revau.lt

Reputation: 2714

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", "[email protected]"),
                new Author("f2", "l2", "[email protected]"),
                new Author("f3", "l3", "[email protected]"),
                new Author("f4", "l4", "[email protected]"));
    }

    private List<Book> getBooks() {
        return asList(
                new Book("The martian", "i1", new LinkedHashSet<>(asList("[email protected]", "[email protected]"))),
                new Book("t2", "i2",
                        new LinkedHashSet<>(asList("[email protected]", "[email protected]", "[email protected]"))));
    }
}

Upvotes: 2

Related Questions