Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: How to create Class<T> parameter?

I'm trying to use the Datastax Java driver API for Cassandra DB, and I have a row object that has the function 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).

My question is how do I actually use this? I don't know how to make a parameter of Class<T> elementsClass type. In my case, the result should be a list of floats (based on the Cassandra schema that I'm using).

like image 531
srlm Avatar asked Mar 23 '23 05:03

srlm


1 Answers

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

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

From JLS 15.8.2 - Class Literals:

A class literal is an expression consisting of the name of a class, interface, array, or primitive type, or the pseudo-type void, followed by a '.' and the token class.

The type of C.class, where C is the name of a class, interface, or array type (§4.3), is Class<C>.

like image 168
Rohit Jain Avatar answered Mar 28 '23 02:03

Rohit Jain