Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the lowest float value from the hash map

Tags:

java

hashmap

map

I have a map which looks like below. What I want to do is get the minimum float value and its corresponding key. Also the float values are like for example 3127668.8 or 1.786453E7 and so on and so forth. How can I achieve this?

Map<String, Float> distance = new HashMap<String, Float>();
like image 883
SASM Avatar asked Dec 09 '22 05:12

SASM


2 Answers

String str;
Float min =Float.valueOf(Float.POSITIVE_INFINITY );
for(Map.Entry<String,Float> e:distance.entrySet()){
    if(min.compareTo(e.getValue())>0){
        str=e.getKey();
        min=e.getValue();
    }
}
like image 169
ratchet freak Avatar answered Dec 27 '22 18:12

ratchet freak


One line of code:

Float min = Collections.min(distance.values());

It's easy to maintain by JDK library.

like image 34
卢声远 Shengyuan Lu Avatar answered Dec 27 '22 20:12

卢声远 Shengyuan Lu