4

我正在尝试为 Cassandra DB 使用 Datastax Java 驱动程序API,并且我有一个具有 getList 函数的行对象:

public <T> List<T> getList(String name,
                          Class<T> elementsClass)

   Returns the value of column name as a list.
Parameters:
   name - the name of the column to retrieve.
   elementsClass - the class for the elements of the list to retrieve.
Returns:
   the value of the ith column in this row as a list of elementsClass objects. If the value is NULL, an empty list is returned (note that Cassandra makes no difference between an empty list and column of type list that is not set).

我的问题是我如何实际使用它?我不知道如何制作Class<T> elementsClass类型参数。在我的例子中,结果应该是一个浮点列表(基于我正在使用的 Cassandra 模式)。

4

1 回答 1

7

要获得List<Float>,您可以使用类文字调用该方法 - Float.class

List<Float> list = getList(someName, Float.class);

JLS 15.8.2 - 类文字

类字面量是由类、接口、数组或原始类型或伪类型 void 的名称组成的表达式,后跟 a'.'和标记类。

的类型C.class,其中C是类、接口或数组类型(第 4.3 节)的名称,是Class<C>

于 2013-09-30T18:31:06.103 回答