Scala Tutorial - Learn How To Use Max Function With Examples
Overview
In this tutorial, we will learn how to use the max function with examples on collection data structures in Scala. The max function is applicable to both Scala's Mutable and Immutable collection data structures.
The max method will iterate through all the elements in a collection and return the largest element.
As per the Scala documentation, the definition of the max method is as follows:
def max: A
The max method is a member of the TraversableOnce trait.
Steps
1. How to initialize a Sequence of donuts
The code below shows how to initialize a Sequence of donut elements where each element in the Sequence is of type String.
println("Step 1: How to initialize a Sequence of donuts")
val donuts: Seq[String] = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut")
println(s"Elements of donuts = $donuts")
You should see the following output when you run your Scala application in IntelliJ:
Step 1: How to initialize a Sequence of donuts
Elements of donuts = List(Plain Donut, Strawberry Donut, Glazed Donut)
2. How to find the maximum element in the sequence using the max function
The code below shows how to use the max method to find the largest donut of type String element in the donut sequence.
println("\nStep 2: How to find the maximum element in the sequence using the max function")
println(s"Max element in the donuts sequence = ${donuts.max}")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to find the maximum element in the sequence using the max function
Max element in the donuts sequence = Strawberry Donut
NOTE:
- Since the donut elements are of type String, the max method is using the natural order of String.
3. How to initialize donut prices
The code below shows how to initialize a Sequence of type Double to represent donut prices.
println("\nStep 3: How to initialize donut prices")
val prices: Seq[Double] = Seq(1.50, 2.0, 2.50)
println(s"Elements of prices = $prices")
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to initialize donut prices
Elements of prices = List(1.5, 2.0, 2.5)
4. How to find the maximum element in the sequence using the max function
The code below shows how to use the max method to find the largest donut price in the Sequence representing donut prices.
println("\nStep 4: How to find the maximum element in the sequence using the max function")
println(s"Max element in the donut prices sequence = ${prices.max}")
You should see the following output when you run your Scala application in IntelliJ:
Step 4: How to find the maximum element in the sequence using the max function
Max element in the donut prices sequence = 2.5
NOTE:
- Since the donut prices elements are of type Double, the max method is using the natural order of type Double.
Summary
In this tutorial, we went over the following:
- How to initialize a Sequence of donuts
- How to find the maximum element in the sequence using the max function
- How to initialize donut prices
- How to find the maximum element in the sequence using the max function
Tip
Source Code
The source code is available on the allaboutscala GitHub repository.
What's Next
In the next tutorial, I will show you how to use maxBy function.