Java: How to create a class parameter <T>?

I am trying to use the Java Datastax API driver for Cassandra DB, and I have a row object that has a getList function

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).

My question is how can I use this? I do not know how to make a type parameter Class<T> elementsClass. In my case, the result should be a list of floats (based on the Cassandra scheme I use).

+4
source share
1 answer

To get List<Float>, you can call the method using the class literal - Float.class:

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

From JLS 15.8.2 - Class Literals :

- , , , - void '.' .

C.class, C - , (§4.3), Class<C>.

+6

All Articles