sachith
sachith

Reputation: 189

List<Long> to List<Integer>

I have a SQL query which returns an array.

List<Integer> listInteger = new ArrayList<>();                                                                   
try (QueryCursor<List<?>> cursor = cache.query(sql_query)) {
    listInteger = (List<Integer>) cursor.getAll().get(0);
}

Then I am trying to get the maximum value element in the array.

int max =  Collections.max(listInteger).intValue();

But this returns java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer error.

Then I tried

int max = (int) Collections.max(ventriTrigeminiBeatList).longValue();

and this also return the same error.

my SQL query is Select sum( timestamp <= t1 and timestamp >= t2), sum( timestamp <= t3 and timestamp >= t4) from db ; I am querying an Ignite cache. Query is working well and it returns an array (ex: [3, 3, 3]). I am not able to use the sql MAX because I am using this listInteger later also.

Could someone tell me how to fix this. Thank You.

Upvotes: 1

Views: 604

Answers (2)

Renis1235
Renis1235

Reputation: 4720

You can get the long maximum and then just cast it to int if needed...

long longMax =  Collections.max(listInteger).longValue();

Integer intMax = longMax == null ? null : Math.toIntExact(longMax); // This throws an exception if an overflow is present.

Upvotes: 0

kidney
kidney

Reputation: 3093

SQL SUM function return type is mapped to Long for integral-type columns in Java, so you'll probably need to change the list to List<Long> and process it then.

See for example https://docs.oracle.com/cd/E19226-01/820-7627/bnbvy/index.html

Upvotes: 3

Related Questions