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

时间:2013-05-28 20:40:12

标签: java string collections

我正在尝试将attributeNames collection of string传递给接受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中将字符串集合转换为字符串数组?有什么想法吗?

1 个答案:

答案 0 :(得分:10)

来自Javadocs中的Collection

  

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

     

返回包含此集合中所有元素的数组;返回数组的运行时类型是指定数组的运行时类型。如果集合适合指定的数组,则返回其中。否则,将使用指定数组的运行时类型和此集合的大小分配新数组。

请注意,此方法接受与集合相同类型的数组;但是,Java不允许您实例化通用数组,因此您需要解决它。有关详细信息,请see this question

相关问题