2016-01-07 143 views
-5

我有一个列表,我想获得百分位数。如何在java中获得百分位数

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"); 

使用以下百分位数公式,我该如何迭代和正确使用? 我想每个键的百分位在哈希表

Number of scores blow X*100/N 
+0

你想要什么不明确的数据?简单地解释 –

+0

我想获得散列图中每个键的百分位数 –

回答

0

所以我还没有下过功夫与HashMaps这样说,但看JavaDoc它应该像

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

我不想要键我得到了ech键的百分位 –

+0

如果我得到了正确的结果,那么只需查找double perc = 1/h.size() ;编辑:nvm,生病改变我的回答 – ShadowPenguin

0

你可以这样做这个(假设百分比是指相对于每个人得分最高的分数,如果你想分配,那么你可能想要使用不同的计算,但这应该给你一个想法)

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)); 
    } 

使用你,你应该得到的东西,如下

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 
+0

如果我需要基于价值? –

相关问题