Martin Moore
Martin Moore

Reputation: 59

Return the median of a column as Long

I am calculating the median of one column of my dataset. I can't find a method to get the median as long (all i get is as double) Here's my attempt :

double median_val = Arrays.stream(dF.stat().approxQuantile("col0", percentiles, 0.0)).findFirst().getAsDouble();

can't find a getAsLong() method, How to achieve that ? Thank you.

Upvotes: 0

Views: 32

Answers (1)

Coursal
Coursal

Reputation: 1387

According to Java 8's (as I'd assume you use at the moment) docs, Stream's findFirst returns an Optional object which does have a toString method, so in a way you can convert the String into long like this:

long median_val = Long.parseLong(Arrays.stream(dF.stat().approxQuantile("col0", percentiles, 0.0)).findFirst().toString());  

However, this seems way too clunky and not at all straightforward (if it works, in some cases), so your best bet is casting the double into a long object by using one of the following ways:

long medianValLong = (long) median_val;               // simple casting
long medianValLongRound = Math.round(median_val);     // rounding off to the nearest long of the double
long medianValLongParse = Long.parseLong(median_val); // same as the first, clanky, way, without involving a String conversion

Upvotes: 1

Related Questions