5

This might be a simple Java streams question. Say, I have a List<Student> object.

public class Student {
    public String name;
    public Set<String> subjects;

    public Set<String> getSubjects() {
        return subjects;
    }
}

How can I get all the subjects taken by the list of students?

I can do this using a for each loop. How can I convert the below code to use Streams?

for (Student student : students) {
    subjectsTaken.addAll(student.getSubjects());
}

Here is my attempt at using Java 8 streams. This gives me an Incompatible types error.

Set<String> subjectsTaken = students.stream()
        .map(student -> student.getSubjects())
        .collect(Collectors.toSet());

3 Answers 3

6

Your current code produces a Set<Set<String>>, not a Set<String>.

You should use flatMap, not map:

Set<String> subjectsTaken = 
    students.stream() // Stream<Student>
           .flatMap(student -> student.getSubjects().stream()) // Stream<String>
           .collect(Collectors.toSet()); // Set<String>
Sign up to request clarification or add additional context in comments.

Comments

5

Try this:

Set<String> subjectsTaken = 
                   students.stream()
                           .map(Student::getSubjects)
                           .flatMap(Set::stream) 
                           .collect(Collectors.toSet());

The idea is to map the students to their subjects first, then flatten the Stream<Set<String>> to Stream<String> and finally collect the stream to a Set.


I would suggest you to use method references instead of lambda expressions where it's possible (if it doesn't degrade readability).

Comments

1

A different alternative using Stream<T>#<R>collect :

students.stream()
    .map(Student::getSubjects)
    .<Set<String>>collect(HashSet::new, Collection::addAll, Collection::addAll)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.