如何仅在 Scala Map 中按值获取最小值 [英] How to get min by value only in Scala Map
本文介绍了如何仅在 Scala Map 中按值获取最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张地图,其中包含 SomeClass ->Double
我想获得与最小值相关联的 SomeClass
.我该怎么做?关系无关紧要,如果这是一个问题,可以任意打破.
I have a map that has SomeClass -> Double
I want to get the SomeClass
associated with the smallest value. How do I go about doing this? Ties do not matter and can be broken arbitrarily if that is an issue.
推荐答案
使用 minBy
:
Map("a" -> 3.0, "b" -> 1.0, "c" -> 2.0).minBy(_._2)._1
这按预期给出了 "b"
.
这篇关于如何仅在 Scala Map 中按值获取最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文