Scala Iterator max() method with example

The max() method belongs to the concrete value members of the class Abstract Iterator. It is defined in the classes IterableOnceOps. It is utilized to find the largest element.

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.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
  
    }
}

chevron_right


Output:

66

Therefore, the largest element of the collection stated is returned.
Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
  
    }
}

chevron_right


Output:

9.9


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.