scala 系列 ---- transform()用法及程式碼示例
阿新 • • 發佈:2021-01-20
transform()方法用於將SortedMap的元素轉換為可變的SortedMap
函式定義:def transform(f: (K, V) => V): SortedMap.this.type
返回型別:It transforms all the elements of the SortedMap and returns them into a mutable SortedMap.
範例1:
// Scala program of transform() // method import scala.collection.immutable.SortedMap // Creating object object baoneng { // Main method def main(args:Array[String]) { // Creating a SortedMap val m1 = SortedMap(3 -> "bn", 4 -> "bmw", 2 -> "benz") // Applying transform method val result = m1.transform((key, value) => value.toUpperCase) // Displays output println(result) } }
輸出:
Map(2 -> BENZ, 3 -> BN, 4 -> BMW)
範例2:
// Scala program of transform() // method import scala.collection.immutable.SortedMap // Creating object object baoneng { // Main method def main(args:Array[String]) { val m1 = SortedMap(3 -> "bn", 4 -> "bmw", 4 -> "benz") val result = m1.transform((key, value) => value.toUpperCase) println(result) } }
輸出:
Map(3 -> BN, 4 -> BENZ)