在任何标准库中都有这样的东西(例如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;
}
Run Code Online (Sandbox Code Playgroud)
我不这么认为,因为你的实现做了两件完全不同的事情:
这两件事情是如此不同,以至于没有一个理智的通用库会将它们放在一个方法中.
| 归档时间: |
|
| 查看次数: |
170 次 |
| 最近记录: |