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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。