transform()方法用於將Map的元素轉換為可變Map。
函數定義: def transform(f: (K, V) => V): Map.this.type
返回類型: It transforms all the elements of the map and returns them into a mutable map.
範例1:
// Scala program of transform()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a map
val m1 = Map(3 -> "geeks", 4 -> "for", 2 -> "cs")
// Applying transform method
val result = m1.transform((key,value) => value.toUpperCase)
// Displays output
println(result)
}
}
輸出:
Map(3 -> GEEKS, 4 -> FOR, 2 -> CS)
範例2:
// Scala program of transform()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a map
val m1 = Map(3 -> "geeks", 4 -> "for", 4 -> "for")
// Applying transform method
val result = m1.transform((key,value) => value.toUpperCase)
// Displays output
println(result)
}
}
輸出:
Map(3 -> GEEKS, 4 -> FOR)
相關用法
- Scala SortedMap transform()用法及代碼示例
- Scala Map min()用法及代碼示例
- Scala Int abs()用法及代碼示例
- Scala Int until(end: Int)用法及代碼示例
- Scala Set ++()用法及代碼示例
- Scala Map get()用法及代碼示例
- Scala Map max()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Scala Map transform() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。