带有示例的 Scala TreeSet product() 方法
product()方法用于返回 TreeSet 的所有元素的乘积。
Method Definition: def product: A
Return Type: It returns the product of all the elements of the 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