I have a HashMap with Strings as keys and values. And I have a String. Now I want to do something like that:
Example
Map<String, Double> storeDistance = new HashMap<String, Double>();
map.put("10-ABCD-01", 15.75);
map.put("10-XYZ-05", 12.13);
map.put("10-ABCD-06", 11.06);
map.put("10-ABCD-04", 10.76);
map.put("10-XYZ-03", 07.62);
Now I Want to find Store ABCD with minimum distance out of all. Here in the case I want to get Store Value as "10-ABCD-04" cause it has minimum distance out of all Keys with Substring ABCD.
I was trying something like below, but it was not working. So any Suggestion here.
storeDistance.keySet().stream()
.filter(key -> key.contains("ABCD")).map(storeDistance::get).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, Math::min));
Use min(Comparator)
on the stream of your key set to get an optional key
String result = storeDistance.keySet().stream().filter(key -> key.contains("ABCD"))
.min(Comparator.comparingDouble(storeDistance::get)).get();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.