scala的breakOut的应用

问题:在scala中,List或Set等集合如何转换为Map集合?

由List/Set转换Map集合时,一般情况下,需要对其元素以元组的形式,再通过toMap等函数实现。

利用collections.breakOut的方式可直接由List/Set转换为Map集合,如:

val breakOut: Map[Int, String] = List("china", "usa", "russia").map(x => (x.length, x))(collection.breakOut)
    def mergeMessage(attr1: mutable.Map[VertexId, Double], attr2: mutable.Map[VertexId, Double]): mutable.Map[VertexId, Double] = {
      (attr1.keySet ++ attr2.keySet).map { key =>  
        val attr1Val = attr1.getOrElse(key, 0.0)    
        val attr2Val = attr2.getOrElse(key, 0.0)    
        key -> (attr1Val + attr2Val)               
      }(collection.breakOut)
    }

英文比较好的可查看:https://stackoverflow.com/questions/1715681/scala-2-8-breakout/1716558#1716558

还是有点不太懂!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章