I am converting my project to java8. How would I write this code in a better way using java8?
List<Bar> bars = new ArrayList<>();
for (Foo foo : obj.getFooList()) {
bars.add(Helper.fooToBar(foo));
}
return detailsVos;
Stream the list, mapping using a method reference, then collect to a list and return it:
return obj.getFooList().stream().map(Helper::fooToBar).collect(Collectors.toList());
Note that "better" has been interpreted as "neater" and "using the Java 8 style".
Also note that this may perform slightly worse than your original code, due to the overhead of using a stream.
Iterator overhead when using a for-each loop…
List<Bar>out of aList<Foo>by invoking a method, orList<Integer>out of aList<String>, the answer is the same, and you can refer to the linked question. Note that, since you're clearly learning the Stream API, you'll likely understand more by reading the stream tutorial, and then reading any answers.