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>();
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();
}
}
One line of code:
Float min = Collections.min(distance.values());
It's easy to maintain by JDK library.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With