从番石榴的分裂器创建字符串[]
有没有比以下更有效的方法从Guava的Splitter创建字符串数组?
Lists.newArrayList(splitter.split()).toArray(new String[0]);
有没有比以下更有效的方法从Guava的Splitter创建字符串数组?
Lists.newArrayList(splitter.split()).toArray(new String[0]);
可能不是更有效率,但更清晰的是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);
}
通过使用,这甚至应该比仅为类型信息创建零长度数组并从中创建正确大小的数组更快。collection.size()
toArray()