Scala SortedSet dropWhile()方法及示例
dropWhile() 方法用于从满足所述条件的排序集中删除最长的前缀元素。
方法定义: def dropWhile(p: (A) => Boolean):SortedSet[A]
返回类型。它返回一个TreeSet,其中包含了从满足所述条件的SortedSet中删除最长的前缀元素后的所有元素。
例子 #1:
// Scala program of dropWhile()
// method
import scala.collection.immutable.SortedSet
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a list
var s1 = SortedSet(1, 3, 5, 4, 2)
// Print the SortedSet
println(s1)
// Applying dropWhile method
var res = s1.dropWhile(x => {x % 2 != 0})
// Displays output
println(res)
}
}
输出。
TreeSet(1, 2, 3, 4, 5)
TreeSet(2, 3, 4, 5)
例子#2。
// Scala program of dropWhile()
// method
import scala.collection.immutable.SortedSet
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a list
var s1 = SortedSet(15, 17, 21)
// Print the SortedSet
println(s1)
// Applying dropWhile method
var res = s1.dropWhile(x => {x % 3 == 0})
// Displays output
println(res)
}
}
输出。
TreeSet(15, 17, 21)
TreeSet(17, 21)
极客教程