Open In App

Scala Sequence

Last Updated : 01 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Sequence is an iterable collection of class Iterable. It is used to represent indexed sequences that are having a defined order of element i.e. guaranteed immutable. The elements of sequences can be accessed using their indexes. Method apply is used for the purpose of indexing. Sequences can also be accessed reversibly using the method reverse and reverseIterator. 
The indices range from 0 to (n – 1) Where, n= the length of the sequence. For the purpose of finding the subsequences, sequences support various methods. Methods such as indexOf, segmentLength, prefixLength, lastIndexWhere, lastIndexOf, startsWith, endsWith. There are two primary subtraits of Sequence namely IndexedSeq and LinearSeq which gives different performance guarantees. IndexexedSeq provides fast and random access of elements while LinearSeq provides fast access to the first element only via head and also contains a fast tail operation. 
Example #1: 
 

Scala




// Scala program to illustrate sequence
import scala.collection.immutable._
 
object GFG
{
    // Main Method
    def main(args:Array[String])
    {
        // Initializing sequence
        var seq:Seq[Int] = Seq(1,2,3,4,5,6)
         
        // Printing Sequence
        seq.foreach((element:Int) => print(element+","))
        println("\nElements Access Using Index")
        println(seq(0))
        println(seq(1))
        println(seq(2))
        println(seq(3))
        println(seq(4))
        println(seq(5))
    }
}


Output

1,2,3,4,5,6,
Elements Access Using Index
1
2
3
4
5
6

Some of the Predefined Methods used in Sequence 
 

  • def apply(index: Int): A -> To select an element from the sequence
  • def contains[A1 >: A](elem: A1): Boolean -> To check whether a sequence contains the given element
  • def count(p: (A)=> Boolean): Int-> To count the number of elements that satisfies a predicate
  • def length: Int -> gives the length of the Sequence
  • def copyToArray(xs: Array[A], start: Int, len: Int): Unit -> For copying the elements of Sequence to array
  • def endsWith[B](that: GenSeq[B]): Boolean-> to check whether a sequence terminates with a given sequence or not
  • def head: A ->It selects the first element of the sequence.
  • def indexOf(elem: A): Int-> To find the index of first occurrence of a value in the sequence
  • def isEmpty: Boolean ->To test the emptiness of the sequence.
  • def lastIndexOf(elem: A): Int-> To find the index of last occurrence of a value in the sequence
  • def reverse: Seq[A]-> To return a new sequence with elements in reverse order.

Sequence Example using Predefined methods 
Example #2: 
 

Scala




// Scala program to illustrate sequence
object MainObject
{
    // Main Method
    def main(args:Array[String])
    {
        // Initializing sequence
        var seq:Seq[Int] = Seq(1, 2, 3, 4, 5, 6)
         
        // Printing Sequence
        seq.foreach((element:Int) => print(element+","))
         
        // Using Some Predefined Methods
        println("\nis Empty: "+ seq.isEmpty)
        println("\nEnds with (5,6): "+ seq.endsWith(Seq(5,6)))
        println("\nLength of sequence: "+ seq.length)
        println("\ncontains 3: "+ seq.contains(3))
        println("\nlast index of 4 : "+ seq.lastIndexOf(4))
        println("\nReversed sequence: "+ seq.reverse)
    }
}


Output

1,2,3,4,5,6,
is Empty: false

Ends with (5,6): true

Length of sequence: 6

contains 3: true

last index of 4 : 3

Reversed sequence: List(6, 5, 4, 3, 2, 1)


Similar Reads

Program to convert Java set of Shorts to an Indexed Sequence in Scala
A java set of Shorts can be converted to an Indexed Sequence in Scala by utilizing toIndexedSeq method of Java in Scala. Here, we need to import Scala’s JavaConversions object in order to make this conversions work else an error will occur. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert
2 min read
Scala | Sequence Comprehensions
Comprehensions have the structure for (enumerators) yield e, wherever enumerators refer to a semicolon-separated list of enumerators. Enumerator is either a generator that introduces new variables, or it’s a filter. A comprehension evaluates the body e for every binding generated by the enumerators and returns a sequence of those values. In this Sc
2 min read
Program to convert Java Set to Sequence in Scala
A java Set can be converted to a Sequence in Scala by utilizing toSeq method of Java in Scala. Here, we need to import Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Java set // to Sequence in Scala // Importing Scala
2 min read
Program to convert Java list of characters to an Indexed Sequence in Scala
A java list of characters can be converted to an Indexed Sequence in Scala by utilizing toIndexedSeq method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work else an error will occur. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to co
2 min read
Program to convert Java list of integers to an Indexed Sequence in Scala
A java list can be converted to an Indexed Sequence in Scala by utilizing toIndexedSeq method of Java in Scala. Here, we need to import Scala’s JavaConversions object in order to make this conversions work else an error will occur. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Java list
2 min read
Program to convert Java list of Strings to an Indexed Sequence in Scala
A java list of Strings can be converted to an Indexed Sequence in Scala by utilizing toIndexedSeq method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work else an error will occur. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to conve
2 min read
Program to convert Java set of integers to an Indexed Sequence in Scala
A java set of integers can be converted to an Indexed Sequence in Scala by utilizing toIndexedSeq method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work else an error will occur. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to conve
2 min read
Program to convert Java set of characters to an Indexed Sequence in Scala
A java set of characters can be converted to an Indexed Sequence in Scala by utilizing toIndexedSeq method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work else an error will occur. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to con
2 min read
Program to convert Java set of Strings to an Indexed Sequence in Scala
A java set of Strings can be converted to an Indexed Sequence in Scala by utilizing toIndexedSeq method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work else an error will occur. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to conver
2 min read
Program to convert Java set of floats to an Indexed Sequence in Scala
A java set of floats can be converted to an Indexed Sequence in Scala by utilizing toIndexedSeq method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work else an error will occur. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert
2 min read
Program to convert Java set of doubles to an Indexed Sequence in Scala
A java set of doubles can be converted to an Indexed Sequence in Scala by utilizing toIndexedSeq method of Java in Scala. Here, we need to import Scala’s JavaConversions object in order to make this conversions work else an error will occur. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert
2 min read
Program to convert Java set of bytes to an Indexed Sequence in Scala
A java set of bytes can be converted to an Indexed Sequence in Scala by utilizing toIndexedSeq method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work else an error will occur. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert
2 min read
Program to convert Java list of floats to an Indexed Sequence in Scala
A java list of floats can be converted to an Indexed Sequence in Scala by utilizing toIndexedSeq method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work else an error will occur. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to conver
2 min read
Program to convert Java list of doubles to an Indexed Sequence in Scala
A java list of doubles can be converted to an Indexed Sequence in Scala by utilizing toIndexedSeq method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work else an error will occur. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to conve
2 min read
Program to convert Java list of Shorts to an Indexed Sequence in Scala
A java list of Shorts can be converted to an Indexed Sequence in Scala by utilizing toIndexedSeq method of Java in Scala. Here, we need to import Scala’s JavaConversions object in order to make this conversions work else an error will occur. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert
2 min read
Program to convert Java list of bytes to an Indexed Sequence in Scala
A java list of bytes can be converted to an Indexed Sequence in Scala by utilizing toIndexedSeq method of Java in Scala. Here, we need to import Scala’s JavaConversions object in order to make this conversions work else an error will occur. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert
2 min read
Program to convert Java Set of characters to Sequence in Scala
A java Set of characters can be converted to a Sequence in Scala by utilizing toSeq method of Java in Scala. Here, we need to import Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Java set // to Sequence in Scala // I
1 min read
Program to convert Java Set of Strings to Sequence in Scala
A java Set of Strings can be converted to a Sequence in Scala by utilizing toSeq method of Java in Scala. Here, we need to import Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Java set // to Sequence in Scala // Impo
1 min read
Program to convert Java Set of floats to Sequence in Scala
A java Set of floats can be converted to a Sequence in Scala by utilizing toSeq method of Java in Scala. Here, we need to import Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Java set // to Sequence in Scala // Impor
1 min read
Program to convert Java Set of doubles to Sequence in Scala
A java Set of doubles can be converted to a Sequence in Scala by utilizing toSeq method of Java in Scala. Here, we need to import Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Java set // to Sequence in Scala // Impo
1 min read
Program to convert Java Set of Shorts to Sequence in Scala
A java Set of Shorts can be converted to a Sequence in Scala by utilizing toSeq method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Java set // to Sequence in Scala // Impo
1 min read
Program to convert Java Set of bytes to Sequence in Scala
A java Set of bytes can be converted to a Sequence in Scala by utilizing toSeq method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Java set // to Sequence in Scala // Impor
1 min read
Scala short <(x: Short): Boolean
Short, a 16-bit signed integer (equivalent to Java's short primitive type) is a subtype of scala.AnyVal. The <(x: Short) method is utilized to return true if this value is less than x, false otherwise. Method Definition: def <(x: Short): Boolean Return Type: It returns true if this value is less than x, otherwise false. Example #1: // Scala p
1 min read
Scala short <(x: Char): Boolean
Short, a 16-bit signed integer (equivalent to Java's short primitive type) is a subtype of scala.AnyVal. The <(x: Char) method is utilized to return true if this value is less than x, false otherwise. Method Definition: def <(x: Char): BooleanReturn Type: It returns true if this value is less than x, otherwise false. Example #1: C/C++ Code //
1 min read
Scala Extractors
In Scala Extractor is defined as an object which has a method named unapply as one of its part. This method extracts an object and returns back the attributes. This method is also used in Pattern matching and Partial functions. Extractors also explains apply method, which takes the arguments and constructs an object so, it's helpful in constructing
6 min read
Scala | Partially Applied functions
The Partially applied functions are the functions which are not applied on all the arguments defined by the stated function i.e, while invoking a function, we can supply some of the arguments and the left arguments are supplied when required. we call a function we can pass less arguments in it and when we pass less arguments it does not throw an ex
3 min read
Scala String indexOf(String str) method with example
The indexOf(String str) method is utilized to return the index of the sub-string which occurs first in the string stated. Method Definition: indexOf(String str) Return Type: It returns the index of the sub-string which is specified in the argument of the method. Example #1: // Scala program of int indexOf() // method // Creating object object GfG {
1 min read
Scala String contentEquals() method with example
The contentEquals() method is utilized to compare a string to the content of StringBuffer. Method Definition: Boolean contentEquals(StringBuffer sb) Return Type: It returns true if the content is equal to the stated string else it returns false. Example #1: // Scala program of contentEquals() // method // Creating object object GfG { // Main method
1 min read
Scala Keywords
Keywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to use as variable names or objects. Doing this will result in a compile-time error. Example: // Scala Program to illustrate the keywords // Here object, def, and var are valid ke
2 min read
Scala Int /(x: Int) method with example
The /(x: Int) method is utilized to return the quotient when the specified first int value is divided by the second int value. Method Definition: (First_Int_Value)./(Second_Int_Value) Return Type: It returns the quotient when the specified first int value is divided by the second int value. Example #1: // Scala program of Int /(x: Int) // method //
1 min read
Article Tags :