Scala Iterator max() 方法与示例
max()方法属于抽象迭代器类的具体值成员。它在IterableOnceOps类中定义。它用于找到最大的元素。
Method Definition : def max[B >: A](implicit ord: math.Ordering[B]): A
Where, B is the type over which the ordering is defined and ord is an ordering to be used for comparing elements.
Return Type :It returns the largest element of the collection stated with respect to the ordering ord.
示例 #1:
// Scala program of max()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Declaring an iterator
val iter = Iterator(22, 36, 66, 19, 21)
// Applying max method
val iter1 = iter.max
// Displays output
println(iter1)
}
}
输出:
66
因此,返回集合的最大元素。
示例 #2:
// Scala program of max()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Declaring an iterator
val iter = Iterator(2.2, 3.6, 6.6, 9.9, 2.1)
// Applying max method
val iter1 = iter.max
// Displays output
println(iter1)
}
}
输出:
9.9