Scala TreeSet product()方法及示例
product() 方法是用来返回TreeSet中所有元素的乘积的。
方法定义: def product:A
返回类型。它返回TreeSet中所有元素的乘积。
例子 #1:
// Scala program of product()
// method
// Import TreeSet
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating TreeSet
val t1 = TreeSet(3, 1, 5, 2, 4)
// Print the TreeSet
println(t1)
// Applying product method
val result = t1.product
// Displays output
print("Product of all elements of the TreeSet: " + result)
}
}
输出。
TreeSet(1, 2, 3, 4, 5)
Product of all elements of the TreeSet: 120
例子#2。
// Scala program of product()
// method
// Import TreeSet
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating TreeSet
val t1 = TreeSet(3, 7, 5, 2)
// Print the TreeSet
println(t1)
// Applying product method
val result = t1.product
// Displays output
print("Product of all elements of the TreeSet: " + result)
}
}
输出。
TreeSet(2, 3, 5, 7)
Product of all elements of the TreeSet: 210