I can offer two paths, but I find your path more direct.
C IntStream, for example:
List<Bar> barList = new ArrayList<>();
IntStream.range(0, fooList.size())
.forEach(i->{
if (i == 0) {
barList.add(new Bar(foo, false));
}else {
barList.add(new Bar(foo, true));
}
}
);
This is not a real functional approach ( forEach()use and no Collector) because it supports the current List index.
Alternatively, you can use a more functional approach, but I do not find it more direct:
List<Bar> barList = IntStream.range(0, fooList.size())
.mapToObj(i->{
Foo foo = fooList.get(i);
if (i == 0) {
return new Bar(foo, false);
}
return new Bar(foo, true));
})
.collect(Collectors.toList());
source
share