Assume we have a list as follows. CoreResult
has a field of type List<Double>
.
final List<CoreResult> list = new LinkedList<>(SOME_DATA);
The objective is to flatten the list after extracting that specific field from each CoreResult
object.
Here are 3 possible options. Is any one of them preferable to the others?
Option 1: extract a field via map()
and flatten inside collector
final List<Double> A = list.stream().map(CoreResult::getField)
.collect(ArrayList::new, ArrayList::addAll, ArrayList::addAll);
Option 2: extract a field via map()
, flatten via flatMap()
, simple collector
final List<Double> B = list.stream().map(CoreResult::getField)
.flatMap(Collection::stream).collect(Collectors.toList());
Option 3: extract a field and flatten in one go via flatMap()
, simple collector
final List<Double> C = list.stream().flatMap(
x -> x.getField().stream()).collect(Collectors.toList());
Would the answer be different if there was no need to extract any field from CoreResult, and instead one wanted to simply flatten a List<List<Double>>
?