I have a Stream<Student[]> object and I need to collect as List<Student>
If I use, obj.collect(Collectors.toList()) then I get List<Student[]>
Is there a better way to get this converted?
How about this? you can using Stream#flatMap to merge all of Student[] in a List together.
stream.flatMap(students-> Stream.of(students)).collect(Collectors.toList());
The documentation says:
The flatMap() operation has the effect of applying a one-to-many transformation to the elements of the stream, and then flattening the resulting elements into a new stream.
Student(e.g. fromList<Student>), then you won't have these problems.Stream<Student[]>shows a flaw in the design. If you have the chance, you should try to fix the design instead of trying to find a workaround that would make the badly designed solution work anyways.