I am trying to calculate 95th Percentile from the data sets which I have populated in my below ConcurrentHashMap. I am interested in finding out how many calls came back in 95th percentile of timeMy Map will look like this and it will always be sorted in ascending order on the keys- In which key - means number of millisecondsvalue - means number of calls that took that much millisecondsMilliseconds    Number0               17021               150362               142623               131904               91375               56356               37427               26288               18999               129810              96311              72712              50313              41514              31115              23516              20417              14018              10919              8320              72For example, from the above data sets, it means1702 calls came back in 0 milliseconds15036 calls came back in 1 millisecondsNow I can calculate the 95th percentile by plugging the above data sets in the Excel sheet. But I was thinking to calculate the percentile in Java code.I know the algorithm will look something like this-Sum all values from the map, calculate 95% of the sum, iterate the map keys in ascending order keeping a running total of values, and when sum equals or exceeds the previously calculated 95% of the total sum, the key should be the 95th percentile I guess.But I am not able to plugin this algorithm in the Java code. Below is the map which will have above datasets.Map<Long, Long> histogram = new ConcurrentHashMap<Long, Long>I am not sure what is the best way to calculate the percentile in Java. I am not sure whether I am algorithm is also correct or not. I am just trying to find out how many calls came back in 95th percentile of time.private static void calculatePercentile() {    for (Long time : CassandraTimer.histogram.keySet()) {    }}Can anyone provide some example how to do that?Any help will be appreciated.Updated code:-Below is the code I have got so far. Let me know if I got everything correct in calculating the 95th percentile-/** * A simple method to log 95th percentile information */private static void logPercentileInfo() {    double total = 0;    for (Map.Entry<Long, Long> entry : CassandraTimer.histogram.entrySet()) {        long value = entry.getKey() * entry.getValue();        total += value;    }    double sum = 0.95*total;    double totalSum = 0;    SortedSet<Long> keys = new TreeSet<Long>(CassandraTimer.histogram.keySet());    for (long key : keys) {        totalSum += CassandraTimer.histogram.get(key);        if(totalSum >= sum) {           //this is the 95th percentile I guess            System.out.println(key);        }    }}
How to calculate percentile in Java without using Library
java