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:
// 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) } } |
chevron_right
filter_none
Output:
Map(2 -> cs, 3 -> geeks)
Example: 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) } } |
chevron_right
filter_none
Output:
Map(3 -> geeks)
Recommended Posts:
- Scala Int <(x: Int) method with example
- Scala Int /(x: Int) method with example
- Scala Int %(x: Int) method with example
- Scala Int !=(x: Int) method with example
- Scala Int *(x: Int) method with example
- Scala Int <=(x: Int) method with example
- Scala Set contains() method with example
- Scala Set &() method with example
- Scala Map last() method with example
- Scala Set take() method with example
- Scala Int +(x: Int) method with example
- Scala Set sum() method with example
- Scala Set min() method with example
- Scala Set max() method with example
- Scala Set map() method with example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



