Scala Collections Tutorial
Selected Reading
- Scala Collections - Discussion
- Scala Collections - Useful Resources
- Scala Collections - Quick Guide
- Scala Collections - zip
- Scala Collections - scan
- Scala Collections - reduce
- Scala Collections - partition
- Scala Collections - map
- Scala Collections - foldRight
- Scala Collections - foldLeft
- Scala Collections - fold
- Scala Collections - flatten
- Scala Collections - flatMap
- Scala Collections - find
- Scala Collections - filter
- Scala Collections - dropWhile
- Scala Collections - drop
- Scala Collections - Stream
- Scala Collections - Stack
- Scala Collections - Seq
- Scala Collections - Tuple
- Scala Collections - Queue
- Scala Collections - Option
- Scala Collections - Iterator
- Scala Collections - ListMap
- Scala Collections - HashMap
- Scala Collections - Map
- Scala Collections - TreeSet
- Scala Collections - HashSet
- Scala Collections - BitSet
- Scala Collections - Set
- Scala Collections - Vector
- Scala Collections - ListSet
- Scala Collections - ListBuffer
- Scala Collections - List
- Scala Collections - ArrayBuffer
- Scala Collections - Array using Range
- Scala Collections - Multi-Dimensional Array
- Scala Collections - Array
- Scala Collections - Environment Setup
- Scala Collections - Overview
- Scala Collections - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Scala Collections - Seq
Scala Collections - Seq
Scala Seq is a trait to represent immutable sequences. This structure provides index based access and various utipty 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 pke 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, initiapze 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)Advertisements