Is there something like this in any standard library (e.g. apache-commons, guava) ?
public static <T> List<T> toList(Iterable<T> iterable) {
if (iterable instanceof List)
return (List<T>)iterable;
if (iterable instanceof Collection)
return new ArrayList<T>((Collection<T>)iterable);
List<T> result = new ArrayList<T>();
for (T item : iterable)
result.add(item);
return result;
}
I don’t think so, because your implementation does two completely different things:
These two things are so different that no sane general-purpose library would throw them together in one method.