Scala Map retain() method with example
The retain() method is utilized to retain all the pairs of the map that satisfies the stated condition.
Method Definition: def retain(p: (A, B) => Boolean): Map.this.type
Return Type: It returns all the “key-value” pairs of the map which satisfies the stated predicate.
Example #1:
object GfG
{
def main(args : Array[String])
{
val m 1 = scala.collection.mutable.Map( 3 - > "geeks" ,
1 - > "for" , 2 - > "cs" )
val result = m 1 .retain((key,value) => key > 1 )
println(result)
}
}
|
Output:
Map(2 -> cs, 3 -> geeks)
Example: 2#
object GfG
{
def main(args : Array[String])
{
val m 1 = scala.collection.mutable.Map( 3 - > "geeks" ,
1 - > "for" , 1 - > "cs" )
val result = m 1 .retain((key,value) => key > 1 )
println(result)
}
}
|
Last Updated :
26 Jul, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...