📜  Scala Map retain() 方法与示例

📅  最后修改于: 2022-05-13 01:54:35.518000             🧑  作者: Mango

Scala Map retain() 方法与示例

retain()方法用于保留满足所述条件的所有映射对。

示例 #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)