If fooService.getFoos() returns List<Foo>.
then you can write this:
List<Foo> fooList = fooService.getFoos();
or this:
List<Foo> fooList = new ArrayList(fooService.getFoos());
Is there any significant difference in the resulting fooList between these two approaches?
Yes – you are creating a completely new
List, containing the elements of the original one. You are duplicating the collection in memory, and iterating it from start to end. You are also not using instance provided by the service, and you can’t modify the original. And finally, you’ve omitted the generics declaration in the 2nd snippet.So use the first option.
Update: you indicated you are not allowed to modify the original list. This is actually a problem of
fooService, not of its clients. If the service is also in your control, returnCollections.unmodifiableList(originalList)– thus clients won’t be able to perform modification operations (on attempt an exception will be thrown)