In my application I use 3rd party library (Spring Data for MongoDB to be exact).
Methods of this library return Iterable
, while the rest of my
From CollectionUtils:
List<T> targetCollection = new ArrayList<T>();
CollectionUtils.addAll(targetCollection, iterable.iterator())
Here are the full sources of this utility method:
public static <T> void addAll(Collection<T> collection, Iterator<T> iterator) {
while (iterator.hasNext()) {
collection.add(iterator.next());
}
}
Here's an SSCCE for a great way to do this in Java 8
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class IterableToCollection {
public interface CollectionFactory <T, U extends Collection<T>> {
U createCollection();
}
public static <T, U extends Collection<T>> U collect(Iterable<T> iterable, CollectionFactory<T, U> factory) {
U collection = factory.createCollection();
iterable.forEach(collection::add);
return collection;
}
public static void main(String[] args) {
Iterable<Integer> iterable = IntStream.range(0, 5).boxed().collect(Collectors.toList());
ArrayList<Integer> arrayList = collect(iterable, ArrayList::new);
HashSet<Integer> hashSet = collect(iterable, HashSet::new);
LinkedList<Integer> linkedList = collect(iterable, LinkedList::new);
}
}
Two remarks
While at it, do not forget that all collections are finite, while Iterable has no promises whatsoever. If something is Iterable you can get an Iterator and that is it.
for (piece : sthIterable){
..........
}
will be expanded to:
Iterator it = sthIterable.iterator();
while (it.hasNext()){
piece = it.next();
..........
}
it.hasNext() is not required to ever return false. Thus in the general case you cannot expect to be able to convert every Iterable to a Collection. For example you can iterate over all positive natural numbers, iterate over something with cycles in it that produces the same results over and over again, etc.
Otherwise: Atrey's answer is quite fine.
As soon as you call contains
, containsAll
, equals
, hashCode
, remove
, retainAll
, size
or toArray
, you'd have to traverse the elements anyway.
If you're occasionally only calling methods such as isEmpty
or clear
I suppose you'd be better of by creating the collection lazily. You could for instance have a backing ArrayList
for storing previously iterated elements.
I don't know of any such class in any library, but it should be a fairly simple exercise to write up.
IteratorUtils from commons-collections
may help (although they don't support generics in the latest stable version 3.2.1):
@SuppressWarnings("unchecked")
Collection<Type> list = IteratorUtils.toList(iterable.iterator());
Version 4.0 (which is in SNAPSHOT at this moment) supports generics and you can get rid of the @SuppressWarnings
.
Update: Check IterableAsList from Cactoos.