运维开发网

scala map排序

运维开发网 https://www.qedev.com 2020-03-20 10:44 出处:网络 作者:运维开发网整理
def main(args: Array[String]): Unit = { val aMap = new scala.collection.mutable.HashMap[String, Double] val a = Array("A", "B", "C", "D") val b = Array(4, 5, 8, 9) for(i <- a.indices
def main(args: Array[String]): Unit = {
    val aMap = new scala.collection.mutable.HashMap[String, Double]

    val a = Array("A", "B", "C", "D")
    val b = Array(4, 5, 8, 9)

    for(i <- a.indices){
      aMap += (a(i) -> b(i)) } // 从小到大(默认) val mapSortSmall = aMap.toList.sortBy(_._2) mapSortSmall.foreach(line => println(line._1 +"\t"+ line._2)) // 从大到小 val mapSortBig = aMap.toList.sortBy(-_._2) }
0

精彩评论

暂无评论...
验证码 换一张
取 消