有没有更有效的方法从 Guava's 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()
创建快一点从中得到一个正确大小的数组。
关于java - 从 Guava 的 Splitter 创建一个 String[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7602943/