Java:如何创建Class <T>参数?

srl*_*rlm 4 java cassandra cql3 datastax-java-driver

我正在尝试为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).
Run Code Online (Sandbox Code Playgroud)

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

Roh*_*ain 7

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

List<Float> list = getList(someName, Float.class);
Run Code Online (Sandbox Code Playgroud)

从JLS 15.8.2开始 - 类文字:

类文字是一个表达式,由类,接口,数组或基本类型的名称或伪类型void组成,后跟a '.'和标记类.

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