how to get 2 biggest value in hashmap? eg. i have collection{ dare = 10, to =20, be =15, different = 10}, if we count manually the first biggest value is 20 that the key is to, and the second biggest value is 15 the key is be, According to Finding Key associated with max Value in a Java Map i use:
Map.Entry<String, Float> max1 = null;
Map.Entry<String, Float> max2 = null;
//searching the first biggest value
for(Map.Entry<String, Float> en : TFIDF.entrySet()){
if (max1 == null || en.getValue().compareTo(max1.getValue()) > 0){
max1 = en;
}
}
//searching the second biggest value
for(Map.Entry<String, Float> en : TFIDF.entrySet()){
if (max2 == null || en.getValue().compareTo(max2.getValue()) <= max1.getValue()){
max2 = en;
}
}
but after i compare with the max1 the value return the min value.