What is the map.last method in Scala?

The map.last function in Scala is used to get the reference of the last element, or key-value pair, in the map.

Figure 1 shows a visual representation of the map.last function.

Figure 1: Visual representation of the map.last function

The following module is required in order to use the function: scala.collection.mutable.Map

Syntax

map_name.last
// where the map_name is the name of the map.

Parameter

This function does not require a parameter.

Return value

The map.last function returns the reference to the set’s last element, or key-value pair.

  • It does not remove that element from the set.
  • If the set is empty, it throws a Error.

Code

The following code shows how to use the map.last function in Scala.

import scala.collection.mutable.Map
object Main extends App {
//creating map with values
val map = scala.collection.mutable.Map(
1 -> "Tom",
2 -> "Alsvin",
3 -> "Eddie"
)
//map_1 elements and length
println("The map elements: " + map);
println("The map last element: " + map.last);
}

Free Resources