Initializing a Set with an Iterable
You can use Guava.
Set<T> set = Sets.newHashSet(iterable);
or to make it read like a sentence static import,
import static com.google.common.collect.Sets.*;
Set<T> set = newHashSet(iterable);
HashSet
constructor relies on more than what Iterable
offers: it wants to know the size
of the collection up front in order to optimally construct the underlying HashMap
. If you have a true, austere Iterable
, which doesn't know its size, then you'll have to realize the Iterable
up front by turning it into a regular Collection
in any of a number of obvious ways.
If, on the other hand, you have a richer object that already knows its size, then it would pay to create a minimalist adapter class that wraps your Iterable
into a collection, implementing just size
in addition to forwarding the call to iterator
.
public class IterableCollection<T> implements Collection<T>
{
private final Iterable<T> iterable;
public IterableCollection(Iterable<T> it) { this.iterable = it; }
@Override public Iterator<T> iterator() { return iterable.iterator(); }
@Override public int size() { return ... custom code to determine size ... }
@Override .... all others ... { throw new UnsupportedOperationException(); }
}