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

還是有點不太懂!!!

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