×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Sequence in Scala

By IncludeHelp Last updated : November 14, 2024

Scala Sequence

A sequence in Scala is a collection that is used to store elements in a definite order. It is indexed data storage that allows sequential access to its elements using the index of the element in it based on 0 index.

It is an iterable collection of class iterable from which it gets its properties. The return an ordered list or Vector.= in which the insertion order is maintained.

Creating a Sequence in Scala

First see how to create a sequence in Scala. The following syntax is used to create a list in Scala,

Syntax

1)  Creating an empty sequence,
    var emptySeq: Seq[data_type] = Seq();
2)  Creating an Sequence with defining the data type,
    var mySeq: Seq[data_type] = Seq(element1, element2, ...)
3)  Creating an Sequence without defining the data type,
    var mySeq = Seq(element1, element2, ...)

Example to create a sequence in Scala

// Program to show how to create a list is Scala

object MyObject {
    def main(args: Array[String]): Unit = {
        // Creating an empty Sequence in Scala 
        println("Creating an Empty Sequence!")
        var empSeq: Seq[Int] = Seq();
        println("Empty Sequence : " + empSeq)
        
        // Creating a Sequence by defining its data type    
        println("\nCreating a Sequence by defining its data type ")
        var mySeq: Seq[String] = Seq("Scala", "JavaScript", "Python");
        println("Sequence of type 'String': " + mySeq)
        
        // Creating a Sequence without defining its data type    
        println("\nCreating a Sequence without defining its data type ")
        var mySeq2 = Seq(4 ,1 , 6, 7, 2)
        println("Sequence without defining its data type: " + mySeq2)
    }
}

Output:

Creating an Empty Sequence!
Empty Sequence : List()

Creating a Sequence by defining its data type 
Sequence of type 'String': List(Scala, JavaScript, Python)

Creating a Sequence without defining its data type 
Sequence without defining its data type: List(4, 1, 6, 7, 2)

Types of Sequence in Scala

There are two types or subtraits of the trait sequence that are used in scala.

They are:

  1. Indexed Sequence: The indexed Sequence or IndexedSeq returns a vector which allows the random and fast access to the elements.
  2. Linear Sequence: The linear Sequence or LinerSeq returns a List which allows the fast access to only the first and last element and rest as sequenced.

Program to illustrate different types of Sequence

// Program to illustrate different types of Sequence

import scala.collection._

    object MyObject {
        def main(args: Array[String]): Unit = {
        
        // Creating a Indexed Sequence in Scala 
        val IndSeq = IndexedSeq(3, 6, 9, 12, 15, 18)
        println("Indexed Sequence: " + IndSeq)
        
        // Creating a Linear Sequence in Scala 
        val LinSeq = LinearSeq(2, 4, 6, 8, 10, 12, 14)
        println("Linear Sequence: " + LinSeq)
    }
}

Output:

Indexed Sequence: Vector(3, 6, 9, 12, 15, 18)
Linear Sequence: List(2, 4, 6, 8, 10, 12, 14)

Commonly used type of sequence is the linear sequence. If you initialize the sequence using the seq keyword only, then a LinerSeq is created.

Operations on Scala Sequence

The sequence in Scala has a lot of predefined functions and operations to be performed on it. Though the elements cannot be changed as it is immutable. But operations to extract elements at a given index, finding length, etc., can be performed.

Program to access element at a given index of the sequence

// Program to access element at a 
// given index of the sequence

object MyObject {
    def main(args: Array[String]): Unit = {
        // Creating a Sequence in Scala 
        val mySeq = Seq(3, 6, 9, 12, 15, 18)
        println("Sequence: " + mySeq)
        
        // Access elements at a specific index
        println("Element at index 0: " + mySeq(0))
        println("Element at index 4: "  + mySeq(4))
    }
}

Output:

Sequence: List(3, 6, 9, 12, 15, 18)
Element at index 0: 3
Element at index 4: 15

Common methods on that are predefined on Sequence

  • length: field that defines the length of the sequence.length: field that defines the length of the sequence.
    Syntax:
    Sequence_name.length
  • reverse: function to return a new sequence that is reverse order.
    Syntax:
    Sequence_name.reverse
  • isEmpty: function to check if the sequence is empty of not.
    Syntax:
    Sequence_name.isEmpty

Program to illustrate the working of some common method of Sequence

// Program to access element at a given index of the sequence

object MyObject {
    def main(args: Array[String]): Unit = {
        // Creating a Sequence in Scala 
        val mySeq = Seq(3, 6, 9, 12, 15, 18)
        println("Sequence: " + mySeq)
        
        // Checking if the sequence is empty or not
        if(mySeq.isEmpty)
        println("\nThe given Sequence is empty")
        else
        println("\nThe given Sequence is not empty")  
        
        // Finding the length of sequence 
        val len = mySeq.length
        println("\nThe length of the seqeunce is " + len)
        
        // Reversing the sequence
        val revSeq = mySeq.reverse
        println("\nThe reverse of the seqeunce is " + revSeq)
    }
}

Output:

Sequence: List(3, 6, 9, 12, 15, 18)

The given Sequence is not empty

The length of the seqeunce is 6

The reverse of the seqeunce is List(18, 15, 12, 9, 6, 3)

There are many more method that are used defined in Scala for the sequence which will see in upcoming articles.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.