Scala Tutorial - Learn How To Use MaxBy Function With Examples

By Nadim Bahadoor | Last updated: March 16, 2018 at 14:37 pm

Overview

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

 

The maxBy method takes a predicate function as its parameter and applies it to every element in the collection to return the largest element.

 

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

def maxBy[B](f: (A) ⇒ B): A

 

The maxBy method is a member of the TraversableOnce trait.

Steps

1. How to create a case class to represent Donut object

The code below shows how to create a case class which we will use to represent a Donut object.


println("Step 1: How to create a case class to represent Donut object")
case class Donut(name: String, price: Double)

 

2. How to create a Sequence of type Donut

The code below shows how to create a Sequence of Donut objects by using the Donut case class which we created in Step 1.


println("\nStep 2: How to create a Sequence of type Donut")
val donuts: Seq[Donut] = Seq(Donut("Plain Donut", 1.5), Donut("Strawberry Donut", 2.0), Donut("Glazed Donut", 2.5))
println(s"Elements of donuts = $donuts")

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


Step 2: How to create a Sequence of type Donut
Elements of donuts = List(Donut(Plain Donut,1.5), Donut(Strawberry Donut,2.0), Donut(Glazed Donut,2.5))

 

3. How to find the maximum element in a sequence of case classes objects using the maxBy function

The code below shows how to use the maxBy function to find the most expensive Donut from the Donut Sequence defined in Step 2.


println("\nStep 3: How to find the maximum element in a sequence of case classes objects using the maxBy function")
println(s"Maximum element in sequence of case class of type Donut, ordered by price = ${donuts.maxBy(donut => donut.price)}")

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


Step 3: How to find the maximum element in a sequence of case classes objects using the maxBy function
Maximum element in sequence of case class of type Donut, ordered by price = Donut(Glazed Donut,2.5)

 

4. How to declare a value predicate function for maxBy function

The code below shows how to define a value function which we will pass to the maxBy function.

println("\nStep 4: How to declare a value predicate function for maxBy function")
val donutsMaxBy: (Donut) => Double = (donut) => donut.price
println(s"Value function donutMaxBy = $donutsMaxBy")

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


Step 4: How to declare a value predicate function for maxBy function
Value function donutMaxBy = <function1>

NOTE:

  • The value function takes a Donut type as input parameter and returns a Double which in this example is the Double value as specified by the Donut's price property.

5. How to find the maximum element using maxBy function and pass through the predicate function from Step 4

The code below shows how find the most expensive Donut by using the maxBy function but also passing through the value function from Step 4.


println("\nStep 5: How to find the maximum element using maxBy function and pass through the predicate function from Step 4")
println(s"Maximum element in sequence using function from Step 3 = ${donuts.maxBy(donutsMaxBy)}")

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


Step 5: How to frind the maximum element using maxBy function and passing through the predicate function from Step 4
Maximum element in sequence using function from Step 3 = Donut(Glazed Donut,2.5)

This concludes our tutorial on Learn How To Use MaxBy 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 create a case class to represent Donut object
  • How to create a Sequence of type Donut
  • How to find the maximum element in a sequence of case classes objects using the maxBy function
  • How to declare a value predicate function for maxBy function
  • How to find the maximum element using maxBy function and pass through the predicate function from Step 4

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

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Technology and Finance Consultant with over 14 years of hands-on experience building large scale systems in the Financial (Electronic Trading Platforms), Risk, Insurance and Life Science sectors. I am self-driven and passionate about Finance, Distributed Systems, Functional Programming, Big Data, Semantic Data (Graph) and Machine Learning.
Other allaboutscala.com tutorials you may like: