java - 从 Guava 的 Splitter 创建一个 String[]

标签 java string guava

有没有更有效的方法从 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/

相关文章:

java - eclipse 4.5.1。 - DTP 插件 - 捆绑 org.eclipse.core.runtime

java - java中的授权引擎

arrays - 在将字符串传递给函数之间感到困惑 (C)

c++ - 格式化 : how to convert 1 to “01” , 2 到 “02” , 3 到 "03",等等

java - Guava MapMaker和JDK ConcurrentMap是否使用读/写锁?

java - InputStream 在 TimerTask 调用 Guava 映射时保持打开状态; GlassFish 提示取消部署

java - KSQL 数据生成 - java.lang.ClassNotFoundException : MonitoringProducerInterceptor

java - 使用 POST 将数据从 Android 发送到 AppEngine Datastore

java - GWT 应用程序中 Guava 模块的正确名称是什么?

ruby - 如何从 Ruby 中的字符串中去除括号?