Scala Collections – Seq

Category : Scala | Sub Category : Scala Programs | By Runner Dev Last updated: 2020-10-08 19:54:29 Viewed : 547


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)

Search
Sub-Categories
Related Articles

Leave a Comment:
SuoT
at 2021-07-25 01:13:02
0
tHzc
at 2021-07-25 06:21:19
0