
- Scala Collections - Home
- Scala Collections - Overview
- Scala Collections - Environment Setup
- Scala Collections - Arrays
- Scala Collections - Array
- Scala Collections - Multi-Dimensional Array
- Scala Collections - Array using Range
- Scala Collections - ArrayBuffer
- Scala Collections - Lists
- Scala Collections - List
- Scala Collections - ListBuffer
- Scala Collections - ListSet
- Scala Collections - Vector
- Scala Collections - Sets
- Scala Collections - Set
- Scala Collections - BitSet
- Scala Collections - HashSet
- Scala Collections - TreeSet
- Scala Collections - Maps
- Scala Collections - Map
- Scala Collections - HashMap
- Scala Collections - ListMap
- Scala Collections - Miscellaneous
- Scala Collections - Iterator
- Scala Collections - Option
- Scala Collections - Queue
- Scala Collections - Tuple
- Scala Collections - Seq
- Scala Collections - Stack
- Scala Collections - Stream
- Scala Collections Combinator methods
- Scala Collections - drop
- Scala Collections - dropWhile
- Scala Collections - filter
- Scala Collections - find
- Scala Collections - flatMap
- Scala Collections - flatten
- Scala Collections - fold
- Scala Collections - foldLeft
- Scala Collections - foldRight
- Scala Collections - map
- Scala Collections - partition
- Scala Collections - reduce
- Scala Collections - scan
- Scala Collections - zip
- Scala Collections Useful Resources
- Scala Collections - Quick Guide
- Scala Collections - Useful Resources
- Scala Collections - Discussion
Scala Collections - Seq
Scala Seq is a trait to represent immutable sequences. This structure provides index based access and various utility methods to find elements, their occurences and subsequences. A Seq maintains the insertion order.
Declaring Seq Variables
The following is the syntax for declaring an Seq variable.
Syntax
val seq: Seq[Int] = Seq(1, 2, 3, 4, 5)
Here, seq is declared as an Seq of numbers. Seq provides commands like the following −
Command
val isPresent = seq.contains(4); val contains = seq.endsWith(Seq(4,5)); var lastIndexOf = seq.lasIndexOf(5);
Processing Seq
Below is an example program of showing how to create, initialize and process Seq −
Example
import scala.collection.immutable.Seq object Demo { def main(args: Array[String]) = { var seq = Seq(1, 2, 3, 4, 5, 3) // Print seq elements seq.foreach{(element:Int) => print(element + " ")} println() println("Seq ends with (5,3): " + seq.endsWith(Seq(5, 3))) println("Seq contains 4: " + seq.contains(4)) println("Last index of 3: " + seq.lastIndexOf(3)) println("Reversed Seq" + seq.reverse) } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
\>scalac Demo.scala \>scala Demo
Output
1 2 3 4 5 3 Seq ends with (5,3): true Seq contains 4: true Last index of 3: 5 Reversed SeqList(3, 5, 4, 3, 2, 1)
Finding Elements
You can find elements in a Seq using methods like indexOf, find, and count. These methods provide a way to locate elements based on conditions.
Example
Try following example for finding element in given sequence -
import scala.collection.immutable.Seq object Demo { def main(args: Array[String]) = { val seq = Seq(1, 2, 3, 4, 5, 3) // Find the index of the first occurrence of an element val index = seq.indexOf(3) println("First index of 3: " + index) // Find the first element that matches a condition val firstEven = seq.find(_ % 2 == 0) println("First even number: " + firstEven.getOrElse("None")) // Count the number of elements that match a condition val count = seq.count(_ > 3) println("Count of elements > 3: " + count) } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
First index of 3: 2 First even number: 2 Count of elements > 3: 2
Filtering Seq
You can filter elements in a Seq using the filter method. This method returns a new Seq containing only the elements that satisfy the predicate.
Example
Try following example for filtering numbers greater than 3 in the given sequence -
import scala.collection.immutable.Seq object Demo { def main(args: Array[String]) = { val seq = Seq(1, 2, 3, 4, 5, 3) // Filter elements greater than 3 val filteredSeq = seq.filter(_ > 3) println("Filtered Seq (elements > 3): " + filteredSeq) } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
Filtered Seq (elements > 3): List(4, 5)
Transforming Seq
You can transform a Seq by applying a transformation function to each element using the map method. This method returns a new Seq with the transformed elements.
Example
Try following example for doubling elements of given sequence -
import scala.collection.immutable.Seq object Demo { def main(args: Array[String]) = { val seq = Seq(1, 2, 3, 4, 5) // Double each element in the Seq val doubledSeq = seq.map(_ * 2) println("Doubled Seq: " + doubledSeq) } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
Doubled Seq: List(2, 4, 6, 8, 10)
Reducing Seq
You can reduce a Seq to a single value using methods like reduceLeft, reduceRight, and foldLeft. These methods apply a binary operation to the elements of the Seq.
Example
Try following example for summing and product of given elements in sequence -
import scala.collection.immutable.Seq object Demo { def main(args: Array[String]) = { val seq = Seq(1, 2, 3, 4, 5) // Sum of all elements in the Seq val sum = seq.reduceLeft(_ + _) println("Sum of elements: " + sum) // Product of all elements in the Seq val product = seq.reduceRight(_ * _) println("Product of elements: " + product) } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
Sum of elements: 15 Product of elements: 120
Converting Seq to Other Collections
You can convert a Seq to other collections such as lists, arrays, and sets. This is useful when you need to use the Seq data in a different context where a different type of collection is required.
Example
Try following example for converting sequence to other collections -
import scala.collection.immutable.Seq object Demo { def main(args: Array[String]) = { val seq = Seq(1, 2, 3, 4, 5) // Convert Seq to List val list = seq.toList // Convert Seq to Array val array = seq.toArray // Convert Seq to Set val set = seq.toSet println("List: " + list) println("Array: " + array.mkString(", ")) println("Set: " + set) } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
List: List(1, 2, 3, 4, 5) Array: 1, 2, 3, 4, 5 Set: Set(5, 1, 2, 3, 4)
Scala Seq Summary
- Seqs in Scala are immutable collections that maintain the insertion order.
- You can perform various operations such as finding elements, filtering, transforming, and reducing Seqs.
- Seqs can be converted to other collections like lists, arrays, and sets.
- Scala provides the Seq trait in the scala.collection.immutable package for creating and manipulating sequences.