java - 如何在java中将字符串集合转换为字符串数组

标签 java string collections

<分区>

我正在尝试将 attributeNames 这是一个 字符串集合 传递给一个方法 setColumnNames 接受 string array.

public Map<String, String> getAttributes(String rowKey, Collection<String> attributeNames, String columnFamily) {

    try {

        SliceQuery<String, String, String> query = HFactory.createSliceQuery(keyspace, StringSerializer.get(), StringSerializer.get(), StringSerializer.get()).
                setKey(rowKey).setColumnFamily(columnFamily).setColumnNames(attributeNames);

    } catch (HectorException e) {
        LOG.error("Exception in CassandraHectorClient::getAttributes " +e+ ", RowKey = " +rowKey+ ", Attribute Names = " +attributeNames);
    }

    return null;
   }

setColumnNames 的定义

SliceQuery<K, N, V> setColumnNames(N... columnNames);

我每次都会遇到这个异常-

The method setColumnNames(String...) in the type SliceQuery<String,String,String> is not applicable for the arguments (Collection<String>)

如何在 Java 中将字符串集合转换为字符串数组?有什么想法吗?

最佳答案

来自 Collection在 Javadocs 中:

<T> T[] toArray(T[] a)

Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.

请注意,此方法接受与集合类型相同的数组;但是,Java 不允许您实例化通用数组,因此您需要解决这个问题。更多信息,see this question .

关于java - 如何在java中将字符串集合转换为字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16801102/

相关文章:

collections - 如果元素可以转换,为什么不能使用 .into() 转换容器?

java - 当我运行 Spring Boot 应用程序时创建 bean 时出错

java - Java 什么时候调用 finalize() 方法?

c - 在C中解析字符串

java - 如何在 Java 中追加字符?

c# - 对静态集合的线程安全访问

java - 组织.hibernate.TransactionException : JDBC begin failed i

java - 无法使用 JPADatabase 从 H2 生成 Jooq 类

c - 在C中,如何存储长字符串(例如密码)

Scala - 集合比较 - 为什么 Set(1) == ListSet(1)?