Guava或Apache Commons Collections中有toArray()的任何通用版本吗?

Mihai:

我要寻找的是使用的通用版本Object[] java.util.Collection.toArray()或较不冗长的替代方案T[] java.util.Collection.toArray(T[] array)我目前可以写:

Collection<String> strings;
String[] array = strings.toArray(new String[strings.size()]);

我正在寻找的是这样的:

@SuppressWarnings("unchecked")
public static <T> T[] toArray(Collection<T> collection, Class<T> clazz) {
    return collection.toArray((T[]) Array.newInstance(clazz, collection.size()));
}

然后我可以将其用作:

String[] array = Util.toArray(strings, String.class);

那么在Guava或Commons Collections中实现了类似的东西吗?

当然,我可以编写自己的代码(上面的代码),它看起来和toArray(T [] array)一样快。

axtavt:

Iterables.toArray() 来自番石榴

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Google Guava与Apache Commons

Apache Commons Bag和Guava MultiSet有什么区别?

Guava与Apache Commons Hash / Equals构建器

是否有可行的通用替代apache.commons.collections.CollectionUtils?

使用Apache Commons Collections中的MultiValueMap

缺少org.apache.commons.collections.CollectionUtils

没有显示Apache Commons库文档

是否有与Apache Commons Collections中的ArrayIterator等效的泛型?

java.lang.NoClassDefFoundError:org / apache / commons / collections / Transformer

Apache Commons Collections MultiValuedMap 按多个字段过滤数据

java.lang.NoClassDefFoundError:Lorg / apache / commons / collections / FastHashMap;

Dart 相当于 Apache Commons Collections Bag

Apache-Commons Commons-Functor的状态

Apache Commons FTP问题

Apache Commons CLI(选项)

Apache Commons FTPClient挂起

Apache Commons Unzip方法?

任何比 Apache Commons 更快的 Java 统计分布库?

使用Apache commons-io IOUtils.closeQuietly安全吗?

apache.commons中带有SwallowedExceptionListener的NoClassDefFoundError

准备Nashorn引擎的Apache Commons池的有效方法

带有Signpost和Apache Commons HTTP的OAuth

ClassNotFoundException:带有Maven的org.apache.commons.cli.ParseException

org.apache.commons.discovery.DiscoveryException:没有为org.apache.commons.logging.LogFactory定义的实现

Apache Commons Configuration:ClassNotFoundException:org.apache.commons.beanutils.DynaBean

Apache Commons CLI参数值

Apache Class Commons Math的NoClassDefFoundError

Apache commons 配置 spring 集成

Apache Commons FTPClient.listFiles