Collect all values of a Set field

Viewed 420

I have a collection which has a field of type Set with some values. I need to create a new set collecting all these values.

I am wondering if this is possible using lambda expressions.

Below is the code line :

Set<String> teacherId = batches.stream()
                               .filter(b -> !CollectionUtils.isEmpty(b.getTeacherIds()))
                               .map(b -> b.getTeacherIds())
                               .collect(Collectors.toSet());

The problem is post map operation, it contains a collection of set of strings. So collect operation returns a Set<Set<String>> but i am looking to aggregate all the values to a single set.

4 Answers
Related