Easy way to convert Iterable to Collection

In JDK 8+, without using any additional libs:

Iterator<T> source = ...;
List<T> target = new ArrayList<>();
source.forEachRemaining(target::add);

Edit: The above one is for Iterator. If you are dealing with Iterable,

iterable.forEach(target::add);

Leave a Comment