I want to convert the result to List for the following code:
List<Task> taskList = projectMap.stream().map(p -> p.getProject().getTasks()).collect(Collector.toList());
but the problem is p.getProject().getTasks() is actually a Set, so I got this error
Type mismatch: cannot convert from List<Set<Task>> to List<Task>
So I also tried to return the result as a Set
Set<Task> taskList = (Set<Task>)projectMap.stream().map(p -> p.getProject().getTasks());
error
java.util.stream.ReferencePipeline$3 cannot be cast to java.util.Set
Is there anyway to convert the result to List ?
or remain the result as Set also fine, my goal is to get the list of Task which located in ProjectMap > Project > Task
Set<Task>
so when you collect as list it becameList<Set<Task>>
, so flat theSet<Task>
using flatMap. Then it will beStream<Task>
now you can collect as list of Task . See the solution below by @Eran