retain()方法用於保留滿足指定條件的所有Map對。
函數定義: def retain(p: (A, B) => Boolean): Map.this.type
返回類型: It returns all the “key-value” pairs of the map which satisfies the stated predicate.
範例1:
// Scala program of retain()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a mutable map
val m1 = scala.collection.mutable.Map(3 -> "geeks",
1 -> "for", 2 -> "cs")
// Applying retain method
val result = m1.retain((key,value) => key > 1)
// Displays output
println(result)
}
}
輸出:
Map(2 -> cs, 3 -> geeks)
示例:2#
// Scala program of retain()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a mutable map
val m1 = scala.collection.mutable.Map(3 -> "geeks",
1 -> "for", 1 -> "cs")
// Applying retain method
val result = m1.retain((key,value) => key > 1)
// Displays output
println(result)
}
}
輸出:
Map(3 -> geeks)
相關用法
- Scala Map get()用法及代碼示例
- Scala Int max()用法及代碼示例
- Scala Set last()用法及代碼示例
- Scala Int abs()用法及代碼示例
- Scala Map take()用法及代碼示例
- Scala Map last()用法及代碼示例
- Scala Int until(end: Int)用法及代碼示例
- Scala Int to(end: Int)用法及代碼示例
- Scala Set ++()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Scala Map retain() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。