Scala - Update Method



This chapter takes you through the update method in Scala. There is a special method used to update elements in mutable collections. The update method is used to assign a new value to an element in a collection and mutable object at a specified index and key. So, objects can be treated as arrays when assigning values.

The update() Method

The update() method is a special method used to modify elements in mutable collections like, arrays, lists, maps, etc. You can assign new values to elements at specific positions and keys. You use the () parentheses to update elements, similar to how you access them.

The update() method is a special method. It changes the value of an element in a collection or container.

Syntax

The syntax of the update method in array in Scala is -

collection(index) = value

This syntax is translated by the compiler to the following method call -

collection.update(index, value)

Example

The following example shows a simple usage of the update method with an array in Scala programming -

object Demo {
   def main(args: Array[String]): Unit = {
      val arr = Array(1, 2, 3, 4, 5)
      println("Original array: " + arr.mkString(", "))

      // Using update method
      arr(2) = 10
      println("Updated array: " + arr.mkString(", "))
   }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

Original array: 1, 2, 3, 4, 5
Updated array: 1, 2, 10, 4, 5

In the example, the update method is used to change the value at index 2 of the array from 3 to 10. Demo object updates an element in an array using the update method.

Defining Custom update() Methods

You can define custom update methods for your own classes. You can update elements using the () syntax.

Syntax

The syntax of the custom update method in Scala is -

class ClassName {
   def update(index: Type, value: Type): Unit = {
      // Update logic
   }
}

Example

The following example shows defining a custom update method for a class in Scala -

class CustomArray(size: Int) {
   private val arr = new Array[Int](size)

   def apply(index: Int): Int = arr(index)

   def update(index: Int, value: Int): Unit = {
      arr(index) = value
   }

   override def toString: String = arr.mkString(", ")
}

object Demo {
   def main(args: Array[String]): Unit = {
      val customArray = new CustomArray(5)
      println("Original custom array: " + customArray)

      // Using custom update method
      customArray(2) = 10
      println("Updated custom array: " + customArray)
   }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

Original custom array: 0, 0, 0, 0, 0
Updated custom array: 0, 0, 10, 0, 0

In the example, CustomArray class defines both apply and update methods. You can access elements, and update elements using the () syntax. The Demo object creates an instance of the CustomArray class and uses its update method.

Using update() with Maps

Maps in Scala also support the update method. You can update the value associated with a key.

Syntax

The syntax of the update method in map in Scala is -

map(key) = value

This syntax is translated by the compiler to the following method call -

map.update(key, value)

Example

The following example shows using the update method with a Map Scala programming -

object Demo {
   def main(args: Array[String]): Unit = {
      var map = scala.collection.mutable.Map("a" -> 1, "b" -> 2, "c" -> 3)
      println("Original map: " + map)

      // Using update method
      map("b") = 20
      println("Updated map: " + map)
   }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

Original map: Map(a -> 1, b -> 2, c -> 3)
Updated map: Map(a -> 1, b -> 20, c -> 3)

In the example, the update method is used to change the value associated with the key "b" in the map from 2 to 20. The Demo object updates a value in a map using the update method.

The update() Method Summary

  • The update method in Scala is a special method. You can change the value of an element in a collection or container using the () syntax.
  • You can define custom update methods for your own classes to enable this syntax.
  • Maps in Scala support the update method. You can update the value associated with a key.
Advertisements