Scala Collections – Seq

Category : Scala | Sub Category : Scala Programs | By Prasad Bonam Last updated: 2020-10-08 14:24:29 Viewed : 6316


Scala Collections – Seq 

def apply[A](elems: A*): Seq[A]

Creates a collection with the specified elements 

Syntax:

object Seq extends SeqFactory[Seq] {

}

 Example:

Following example illustrates about Scala Seq

Save the file as −  SeqExample.scala

 SeqExample.scala  

package runnerdev 

object SeqExample extends App {

  var seqSeq[Int] = Seq(1234567)

  seq.foreach((element: Int) => print(element + " "))

  println(" is Empty: " + seq.isEmpty)//false

  println("Ends with (2,7): " + seq.endsWith(Seq(27))) //false

  println("contains 3: " + seq.contains(3)) //true

  println("last index of 3 : " + seq.lastIndexOf(3)) //2

  println("Reverse order of sequence: " + seq.reverse

}

 

 compile and run the above example as follows 

scala> scalac SeqExample.scala

scala> scala SeqExample 

Output:

1 2 3 4 5 6 7

is Empty: false

Ends with (2,7): false

            contains 3: true

            last index of 3 : 2

            Reverse order of sequence: List(7, 6, 5, 4, 3, 2, 1)

In Scala, a Seq (short for "sequence") is a common trait in the collections hierarchy. It represents an ordered sequence of elements where duplicate elements are allowed. Seq is a more general collection type compared to List, as it encompasses various types of sequences, including lists, arrays, and other sequence-like data structures. Here is an overview of Seq and how to use it:

1. Creating a Seq:

You can create a Seq in Scala using various methods:

  • Using Seq() constructor:

    scala
    val seq1: Seq[Int] = Seq(1, 2, 3, 4, 5)
  • Using the List constructor (a specific subtype of Seq):

    scala
    val seq2: Seq[Int] = List(1, 2, 3, 4, 5)
  • Using Seq.apply method:

    scala
    val seq3: Seq[Int] = Seq.apply(1, 2, 3, 4, 5)

2. Accessing Elements:

You can access elements in a Seq using indexing (zero-based) just like you do with arrays or lists:

scala
val firstElement = seq1(0) // Access the first element (1)

3. Common Operations:

Seq supports various common operations, including:

  • Concatenation:

    scala
    val combinedSeq = seq1 ++ Seq(6, 7, 8)
  • Mapping Elements:

    scala
    val squaredSeq = seq1.map(x => x * x)
  • Filtering Elements:

    scala
    val evenNumbers = seq1.filter(_ % 2 == 0)
  • Sorting Elements:

    scala
    val sortedSeq = seq1.sorted

4. Immutability:

Seq is an immutable collection, meaning you cannot modify its elements once created. Instead, you create new sequences when performing operations.

5. Pattern Matching:

You can use pattern matching to destructure and process elements in a Seq:

scala
seq1 match { case head +: tail => println(s"Head: $head, Tail: $tail") case Nil => println("Empty sequence") }

In this example, we pattern match to extract the head and tail of the sequence or handle an empty sequence.

Seq is a versatile collection that provides an ordered sequence of elements with a wide range of operations. It is suitable for various use cases where you need to work with ordered collections of data.


Search
Related Articles

Leave a Comment: