-5

I have a list where I want to get percentile.

HashMap<Integer,String> test=new HashMap<Integer,String>();  
test.put(100,"Amit");  
test.put(101,"Vijay");  
test.put(102,"Rahul"); 
test.put(103,"Amit");  
test.put(104,"Vijay");  
test.put(105,"Rahul");

Using following formula for percentile, how can I iterate and use properly? i want to get percentile of each keys in hash map

Number of scores blow X*100/N
Shakti Sharma
  • 2,131
  • 3
  • 18
  • 23

2 Answers2

0

So i haven worked that much with HashMaps, but looking at the JavaDoc it should be something like

Hashmap<Integer><String> h;
int sum = 0;
for(int i: h.keySet()) {
  sum += i;
}
double percentile = sum/h.size()
ShadowPenguin
  • 106
  • 1
  • 9
0

You can do something like this (assuming by percentile you mean relative to the highest score what everyone scored, if you want distribution then you may want to use different computation but this should give you an idea)

    int maxScore = 0;
    for ( Integer score : test.keySet()) {
         if (score > maxScore) {
             maxScore = score;
         }
    }
    for ( Integer score : test.keySet()) {

         System.out.print(String.format("%s's percentile %5.2f\n", test.get(score), 
                 ((double)score/(double)maxScore)*100));
    }

Using the data you have you should get something as below

Rahul's percentile 97.14
Amit's percentile 98.10
Amit's percentile 95.24
Vijay's percentile 96.19
Vijay's percentile 99.05
Rahul's percentile 100.00
Aniruddh Dikhit
  • 662
  • 4
  • 10