
- Scala - Home
- Scala - Overview
- Scala - Features
- Scala - Environment Setup
- Scala - Build Tool (SBT)
- Scala - REPL
- Scala - Dot & Dotty
- Scala - Basic Syntax
- Scala - Hello World Program
- Scala - Identifiers
- Scala - Keywords
- Scala - Comments
- Scala - Code Blocks
- Scala - Semicolon
- Scala - Constructs
- Scala - Expressions
- Scala - Input and Output
- Scala - Optional Braces
- Scala - Underscore (_)
- Data Types and Variables
- Scala - Data Types
- Scala - Type Bounds
- Scala - Context Bound
- Scala - Variances
- Scala - Type Hierarchy
- Scala - Variables
- Scala - Variable Scopes
- Scala - Literals
- Scala - Numeric Types
- Scala - Boolean Types
- Scala - Char Type
- Scala - Unit Types
- Scala - Strings
- Scala - Arrays
- Scala - Null Type
- Scala - Nothing
- Scala - Any Type
- Scala - AnyRef Type
- Scala - Unified Types
- Scala - Dates and Times
- Scala - Ranges
- Scala - Multidimensional Arrays
- Scala - WrappedArray
- Scala - StringBuilder
- Scala - String Interpolation
- Scala - StringContext
- Scala - Type Casting
- Scala var vs val
- Scala Operators
- Scala - Operators
- Scala - Rules for Operators
- Scala - Arithmetic Operators
- Scala - Relational Operators
- Scala - Logical Operators
- Scala - Bitwise Operators
- Scala - Assignment Operators
- Scala - Operators Precedence
- Scala - Symbolic Operators
- Scala - Range Operator
- Scala - String Concatenation Operator
- Scala Conditional Statements
- Scala - IF ELSE
- Scala - IF-ELSE-IF-ELSE Statement
- Scala - Nested IF-ELSE Statement
- Scala Loop Statements
- Scala - Loop Statements
- Scala - while Loop
- Scala - do-while Loop
- Scala - Nested Loops
- Scala - for Loop
- Scala - break Statement
- Scala - yield Keyword
- Scala Classes & Objects
- Scala - Classes & Objects
- Scala - Constructors
- Scala - Auxiliary Constructor
- Scala - Primary Constructor
- Scala - This Keyword
- Scala - Nested Classes
- Scala - Getters and Setters
- Scala - Object Private Fields
- Scala - Singleton Object
- Scala - Companion Objects
- Scala - Creating Executable Programs
- Scala - Stateful Object
- Scala - Enumerations
- Scala - Polymorphism
- Scala - Access Modifiers
- Scala - Apply Method
- Scala - Update Methods
- Scala - UnapplySeq Method
- Scala - Inheritance
- Scala - Extending a Class
- Scala - Method Overloading
- Scala - Method Overriding
- Scala - Generic Classes
- Scala - Generic Functions
- Scala - Superclass Construction
- Scala Methods & Functions
- Scala - Functions
- Scala - Main Methods
- Scala - Functions Call-by-Name
- Scala - Functions with Named Arguments
- Scala - Function with Variable Arguments
- Scala - Recursion Functions
- Scala - Default Parameter Values
- Scala - Functions without Parameters
- Scala - Implicit Parameters
- Scala - Higher-Order Functions
- Scala - Nested Functions
- Scala - Extension Methods
- Scala - Anonymous Functions
- Partially Applied Functions
- Scala - Lazy Val
- Scala - Pure Function
- Scala - Currying Functions
- Scala - Control Abstractions
- Scala - Corecursion
- Scala - Unfold
- Scala - Tail Recursion
- Scala - Infinite Sequences
- Scala - Dynamic Invocation
- Scala - Lambda Expressions
- Scala Collections
- Scala - Collections
- Mutable and Immutable Collections
- Scala - Lists
- Scala - Sets
- Scala - Maps
- Scala - TreeMap
- Scala - SortedMap
- Scala - Tuples
- Scala - Iterators
- Scala - Options
- Scala - Infinite Streams
- Scala - Parallel Collections
- Scala - Algebraic Data Types
- Scala Pattern Matching
- Scala - Pattern Matching
- Scala - Type Patterns
- Scala - Exception Handling
- Scala - Extractors
- Scala - Regular Expressions
- Scala Files I/O
- Scala - Files I/O
- Scala Advanced Concepts
- Scala - Closures
- Scala - Futures
- Scala - Promises
- Scala - Traits
- Scala - Trait Mixins
- Scala - Layered Traits
- Scala - Trait Linearization
- Scala - Sealed Traits
- Scala - Transparent Traits
- Scala - Literal Type Arithmetic
- Scala - Inline keyword
- Scala - Def, Var & Val
- Scala - Dropped Features
- Scala - BDD Testing
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.