Scala Mutable SortedMap的过滤方法filter()及其示例
filter()方法用于选择所有满足所述谓词的SortedMap元素。
方法定义:def filter(p: ((A, B)) => Boolean): SortedMap[A, B]
返回类型:返回一个新的SortedMap,其中包含满足给定谓词的所有SortedMap元素。
示例1:
// Scala program of filter()
// method
import scala.collection.SortedMap
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating SortedMap
val m1 = SortedMap("geeks" -> 5, "for" -> 3)
// Applying filter method
val result = m1.filter(x => x._1 == "geeks" && x._2 == 5)
// Displays output
println(result)
}
}
Map(geeks -> 5)
示例2:
// Scala program of filter()
// method
import scala.collection.SortedMap
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating SortedMap
val m1 = SortedMap("geeks" -> 5, "for" -> 3)
// Applying filter method
val result = m1.filter(x => x._1 == "for" && x._2 == 5)
// Displays output
println(result)
}
}
Map()
这里返回了一个空的SortedMap,因为没有任何元素满足所述谓词。
阅读更多:Scala 教程
极客教程