Updated April 12, 2023
Introduction to Scala Seq
Scala sequence is the part of the collection and they are the special case of iterable class. As the name suggests they maintain the sequence of the element. They maintain an index sequence that represents a specified order of elements that means immutable. Scala sequence maintains the insertion order of the elements by using the index only. The range of index starts from 0 and goes up to the number of elements the same way as array works.
Syntax
var meseq:Seq[Int] = Seq(value1, value2, value3.....n)
In the above syntax, we are defining the data type for elements also inside seq. Below we can see one practice example how to assign them
var meseq:Seq[Int] = Seq(20, 40, 60, 80, 100)
Scala Seq Methods
Scala sequences are immutable because they work on the basis of the index, of the searching, and filtering operations are very fast we can access any element on the basis of the index. If we want to access the element sequence in reverse order, we have two methods reverse Iterator and reverse.
Scala sequence has two trails LinerSeq and indexedSeq. These both give a guarantee about performance. If you want to search or filter element, then we can go for indexdeSeq it provides as random access of element. But LinerSeq provides fast access only in the case of head and tail search of an element means only the first and last search of the element.
Before we discuss the methods first understand the flow of seq;
1. It has various supertypes;
PartialFunction[Int, A] | Iterable[A] with GenSeq[A] | GenericTraversableTemplate[A, Seq] | SeqLike[A, Seq[A]] | GenIterable[A] |
Parallelizable[A, ParSeq[A]] | FilterMonadic[A, Seq[A]] | HasNewBuilder[A, Seq[A] | GenTraversable[A] | TraversableOnce[A |
Equals | IterableLike[A, Seq[A]] | GenTraversableOnce[A] | NA | NA |
2. subclasses;
AbstractSeq | LinearSeq | SeqView | SeqViewLike | Appended |
DroppedWhile | EmptyView | Filtered | FlatMapped | Forced |
Mapped | Patched | Prepended | Reversed | TakenWhile |
Filtered | ArrayBuffer | IndexedSeqView | UnrolledBuffer | BufferProxy |
StreamView | StreamViewLike | Appended | DroppedWhile | EmptyView |
DoubleLinkedList | LinkedList | ObservableBuffer | QueueProxy | Stack |
ofBoolean | ofByte | ofChar | ofDouble | ofFloat |
Types of Method Scala Seq
Scala seq has two types of methods available
- Concrete
- Abstract
1. foreach()
This method is used to iterate the elements of the sequence and print them.
Code:
var mySeq:Seq[Int] = Seq(20, 40 , 60 , 80 , 100)
// sequence output
mySeq.foreach((element:Int) => println( "element is ::" +element))
}
Output:
2. isEmpty
This method is used to check the seq if it is empty or not. It returns true or false. If the seq is empty it will return true, else it will return false.
Code:
var mySeq:Seq[Int] = Seq(20, 40 , 60 , 80 , 100)
// sequence output
var result = mySeq.isEmpty
println("result of sequence is :: " + result)
}
Output:
3. endsWith()
This method returns true and false. It checks whether the specified sequence ends with the passing parameter or not and based on the result return true and false.
Code:
var mySeq:Seq[Int] = Seq(20, 40 , 60 , 80 , 100)
// sequence output
var result = mySeq.endsWith(Seq(100))
println("result of sequence for endsWith is :: " + result)
}
Output:
4. length()
This method is used to check the number of elements present in the seq. This returns an integer value as the length of seq.
Code:
var mySeq:Seq[Int] = Seq(20, 40 , 60 , 80 , 100)
// sequence output
var result = mySeq.length
println("result of sequence for length is :: " + result)
}
Output:
5. contains()
This method is used to check the value present in the seq or not. It returns Boolean true or false.
Code:
var mySeq:Seq[Int] = Seq(20, 40 , 60 , 80 , 100)
// sequence output
var result = mySeq.contains(80)
println("result of sequence for contains is :: " + result)
}
Output:
6. lastIndexOf()
This method is used to return the index of the value when it last occurred in the sequence list. It returns an integer value as its index.
Code:
var mySeq:Seq[Int] = Seq(20, 40 , 60 , 80 , 100)
// sequence output
var result = mySeq.lastIndexOf(60)
println("result of sequence for lastIndexOf is :: " + result)
}
Output:
7. reverse()
This method print the sequence in the reverse order.
Code:
var mySeq:Seq[Int] = Seq(20, 40 , 60 , 80 , 100, 60)
// sequence output
var result = mySeq.reverse
println("result of sequence for reverse is :: " + result)
}
Output:
8. head
This method returns the first element from the sequence.
Code:
var mySeq:Seq[Int] = Seq(20, 40 , 60 , 80 , 100, 60)
// sequence output
var result = mySeq.head
println("result of sequence for head is :: " + result)
}
Output:
9. indexOf()
This method return the index of element present in the sequence.
Code:
var mySeq:Seq[Int] = Seq(20, 40 , 60 , 80 , 100, 60)
// sequence output
var result = mySeq.indexOf(80)
println("result of sequence for indexOf is :: " + result)
}
Output:
10. last()
This method return the last element from the sequence.
Code:
var mySeq:Seq[Int] = Seq(20, 40 , 60 , 80 , 100, 60)
// sequence output
var result = mySeq.last
println("result of sequence for last is :: " + result)
}
Output:
Conclusion
Scala sequence maintains the insertion order hence they are immutable. They are best suited for searching ad filtering but the insertion and deletion in middle can cost us more and the performance will also reduce because we need to shift the elements of sequence accordingly. They are based on the index mapping of elements like array
Recommended Articles
We hope that this EDUCBA information on “Scala Seq” was beneficial to you. You can view EDUCBA’s recommended articles for more information.