Scala Tutorial - Learn How To Use Min Function With Examples

By Nadim Bahadoor | Last updated: November 20, 2017 at 21:27 pm

Overview

In this tutorial, we will learn how to use the min function with examples on collection data structures in Scala. The min function is applicable to both Scala's Mutable and Immutable collection data structures.

 

The min method will iterate through all the elements in the collection and return the smallest element.

 

As per the Scala documentation, the definition of the min method is as follows:

def min: A

 

The min 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 donuts 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 minimum element in the sequence using the min function

The code below shows how to use the min function to find the minimum element of a Sequence.


println("\nStep 2: How to find the minimum element in the sequence using the min function")
println(s"Min element in the donuts sequence = ${donuts.min}")

You should see the following output when you run your Scala application in IntelliJ:


Step 2: How to find the minimum element in the sequence using the min function
Min element in the donuts sequence = Glazed Donut

 

NOTE:

  • Given that the donut sequence is of type String, the min function is using the natural order of type String.

3. How to initialize a Sequence of donut prices

The code below shows how to declare a Sequence of donut prices of type Double.


println("\nStep 3: How to initialize a Sequence of 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 a Sequence of donut prices
Elements of prices = List(1.5, 2.0, 2.5)

 

4. How to find the minimum element in the sequence using the min function

The code below shows how to use the min function to find the minimum element in a Sequence of type Double.


println("\nStep 4: How to find the minimum element in the sequence using the min function")
println(s"Min element in the donut prices sequence = ${prices.min}")

You should see the following output when you run your Scala application in IntelliJ:


Step 4: How to find the minimum element in the sequence using the min function
Min element in the donut prices sequence = 1.5

NOTE:

  • Since the prices Sequence is of type double, the min function is using the natural ordering of type Double.

This concludes our tutorial on Learn How To Use Min Function With Examples and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

Summary

In this tutorial, we went over the following:

  • How to initialize a Sequence of donuts
  • How to find the minimum element in the sequence using the min function
  • How to initialize a Sequence of donut prices
  • How to find the minimum element in the sequence using the min function

Tip

  • Review the tutorials on Mutable and Immutable collection data structures in Scala.

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 minBy function.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Senior Principal Engineer | Elsevier
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on