sli*_*eed 26 java string guava
是否有更有效的方法从Guava的Splitter创建一个字符串数组而不是以下?
Lists.newArrayList(splitter.split()).toArray(new String[0]);
Run Code Online (Sandbox Code Playgroud)
Phi*_*art 27
可能没有那么高效,但会更清楚 Iterables.toArray(Iterable, Class)
这几乎就是你做的事情:
public static <T> T[] toArray(Iterable<? extends T> iterable, Class<T> type) {
Collection<? extends T> collection = toCollection(iterable);
T[] array = ObjectArrays.newArray(type, collection.size());
return collection.toArray(array);
}
Run Code Online (Sandbox Code Playgroud)
通过使用collection.size()它,甚至可以比为类型信息创建零长度数组并且toArray()从中创建正确大小的数组更快.
Jas*_*n S 17
怎么样
Iterables.toArray(splitter.split(), String.class);
Run Code Online (Sandbox Code Playgroud)
因为有一种Iterables.toArray()方法