Scala 不可变 TreeSet clear() 方法
在 Scala 不可变TreeSet class
中,使用clear()方法删除 TreeSet 的所有元素。
Method Definition: def clear(): Unit
Return Type: It returns an empty TreeSet.
示例 #1:
// Scala program of clear()
// method
// Import TreeSet
import scala.collection.immutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating TreeSet
val t1 = TreeSet(2, 1, 3, 4, 5)
// Print the TreeSet
println("Before clear method: " + t1)
// Applying clear() method
val result = t1.clear
// Displays output
println("After clear method: " + result)
}
}
输出:
Before clear method: TreeSet(1, 2, 3, 4, 5)
After clear method: ()
示例 #2:
// Scala program of clear()
// method
// Import TreeSet
import scala.collection.immutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating TreeSet
val t1 = TreeSet("a", "e", "i", "o", "u")
// Print the TreeSet
println("Before clear method: " + t1)
// Applying clear() method
val result = t1.clear
// Displays output
println("After clear method: " + result)
}
}
输出:
Before clear method: TreeSet(a, e, i, o, u)
After clear method: ()