title: How to calculate 95 percentile in Java using the datasets in Map
body: <p>I am trying to calculate <code>95% percentile</code> from my datasets in Java. My datasets will have something like this-</p>
  
  <p>I will be having a <code>ConcurrentHashMap</code> that will have key value pair like this-</p>
  
  <pre><code>Key = 30
  Value = 10
  </code></pre>
  
  <p>which means 10 calls came back in 30 milliseconds</p>
  
  <p>Another Example-</p>
  
  <pre><code>Key = 20
  Value = 5
  </code></pre>
  
  <p>which means , 5 calls came back in 20 milliseconds</p>
  
  <p>So from the above Map, I am trying to calculate 95% Percentile in Java.</p>
  
  <p>Can anyone provide any example how to do that in Java from my above Map? Thanks for the help</p>
  
  <p><strong>Updated Code:-</strong></p>
  
  <p>Below is the code I have got which will calculate the 95th percentile from the Map-</p>
  
  <pre><code>/**
   * A simple method to log 95th percentile information
   */
  private static void logPercentileInfo() {
  
      double total = 0;
      for (Map.Entry&lt;Long, Long&gt; entry : CassandraTimer.histogram.entrySet()) {
          long value = entry.getKey() * entry.getValue();
          total += value;
      }
  
      double sum = 0.95*total;
  
      double totalSum = 0;
  
      SortedSet&lt;Long&gt; keys = new TreeSet&lt;Long&gt;(CassandraTimer.histogram.keySet());
      for (long key : keys) {
  
          totalSum += CassandraTimer.histogram.get(key);
  
          if(totalSum &gt;= sum) {
              System.out.println(key);
          }
      }
  
  }
  </code></pre>
  
  <p>Can anyone take a look and let me know whether I have written correctly?</p>