Scala Trait Traversable | Set-3
Prerequisite :
Scala Trait Traversable | Set-1
Scala Trait Traversable | Set-2
It is recommended to see (Set-1, Set-2) before this Set.
The operations are as follows:
- Sub-Collection retrieval operations:
The operations here are slice, drop, dropWhile, filter, filterNot, tail, take, takeWhile, and init. These operations are utilized to return some sub collection.
Example :
object SubCollection
{
def main(args : Array[String])
{
val x = List( 17 , 19 , 21 , 29 , 31 )
val y = x.init
println(y)
}
}
|
Output:
List(17, 19, 21, 29)
Here, init retrieves all the elements of the Traversable collection except the last element.
Example :
object SubCollection
{
def main(args : Array[String])
{
val x = List( 17 , 19 , 21 , 29 , 31 )
val y = x.tail
println(y)
}
}
|
Output:
List(19, 21, 29, 31)
Here, tail retrieves all the elements of the Traversable collection except the first element.
Example :
object SubCollection
{
def main(args : Array[String])
{
val x = List( 27 , 29 , 31 , 49 , 51 , 56 )
val y = x.slice( 2 , 5 )
println(y)
}
}
|
Here, slice will return the elements from the given range of index excluding last index.
Example :
object SubCollection
{
def main(args : Array[String])
{
val x = List( 37 , 49 , 51 , 69 , 71 , 86 )
val y = x.take( 2 )
println(y)
}
}
|
Here, this operation will return numbers of elements given in the argument of the take operation.
Example :
object SubCollection
{
def main(args : Array[String])
{
val x = List( 33 , 34 , 36 , 37 , 39 , 40 , 44 )
val y = x.drop( 4 )
println(y)
}
}
|
Here, this operation will return all the elements of the collection except the first number of elements given in the argument of drop operation.
Example:
object SubCollection
{
def main(args : Array[String])
{
val x = List( 2 , 5 , 9 , 13 , 17 , 20 )
val y = x.dropWhile( _ < 10 )
println(y)
}
}
|
Here, dropWhile will drop the elements until the given condition is satisfied and returns rest of the left elements.
Example :
object SubCollection
{
def main(args : Array[String])
{
val x = List( 2 , 5 , 9 , 10 , 13 , 17 , 20 )
val y = x.takeWhile( _ < 13 )
println(y)
}
}
|
Output:
List(2, 5, 9, 10)
Here, takeWhile will take the elements until the given condition is satisfied and returns these elements.
Example :
object SubCollection
{
def main(args : Array[String])
{
val x = List( 9 , 65 , 99 , 10 , 23 , 17 , 12 )
val y = x.filter( _ < 23 )
println(y)
}
}
|
Output:
List(9, 10, 17, 12)
Here, filter will return all the elements until the given condition is satisfied and drops the rest.
Example :
object SubCollection
{
def main(args : Array[String])
{
val x = List( 9 , 65 , 99 , 10 , 23 , 17 , 12 )
val y = x.filterNot( _ < 23 )
println(y)
}
}
|
Here, filterNot will return all the elements which does not satisfies the given condition and drops the rest.
- Subdivision operations:
These operations include span, partition, splitAt, groupBy. These operations are utilized to break the elements of the collection and return some sub-collections.
Example :
object Subdivision
{
def main(args : Array[String])
{
val q = List( 7 , 9 , 11 , 15 , 17 , 19 , 22 )
val r = q.span( _ < 15 )
println(r)
}
}
|
Output:
(List(7, 9, 11), List(15, 17, 19, 22))
Here, span will split the given Traversable collection of element into two parts where, first part is obtained by performing takeWhile operation and second part is obtained by performing dropWhile operation.
Example :
object Subdivision
{
def main(args : Array[String])
{
val q = List( 17 , 29 , 31 , 36 , 37 , 39 , 42 )
val r = q.partition( _ < 35 )
println(r)
}
}
|
Output:
(List(17, 29, 31), List(36, 37, 39, 42))
Here, partition will divide the collection into two parts where, first part will return the elements till the given condition is satisfied and second part will return rest of the elements.
Example :
object Subdivision
{
def main(args : Array[String])
{
val q = List( 17 , 29 , 31 , 36 , 37 , 39 , 42 )
val r = q.splitAt( 4 )
println(r)
}
}
|
Output:
(List(17, 29, 31, 36), List(37, 39, 42))
Here, splitAt will divide the elements of the collection into two parts in the given position where, first part will be obtained by performing take operation and second part will be obtained by performing drop operation.
Example :
object Subdivision
{
def main(args : Array[String])
{
val x = List( 21 , 20 , 33 , 40 , 27 , 62 )
val y = x.groupBy
{
case z : Int if (z % 3 == 0 ) => z + 3
case z : Int if (z % 2 == 0 ) => z + 2
}
println(y)
}
}
|
Output:Map(42 -> List(40), 24 -> List(21), 64 -> List(62), 22 -> List(20), 36 -> List(33), 30 -> List(27))
Here, groupBy will divide the Traversable collection on the basis of the partial function given and will return a Map.
- Element test methods:
These methods include forall, exists, and count. These methods are utilized to test the given collection of Traversable on the basis of stated conditions.
Example :
object Elementtest
{
def main(args : Array[String])
{
val x = List( 21 , 20 , 33 , 40 , 27 , 62 )
val y = x forall ( _ < 63 )
println(y)
}
}
|
Here, forall will return true if all the elements of the collection satisfies the given condition else returns false.
Example :
object Elementtest
{
def main(args : Array[String])
{
val x = List( 21 , 20 , 33 , 40 , 27 , 62 )
val y = x exists ( _ < 30 )
println(y)
}
}
|
Here, exists will return true if some of the elements of the collection satisfies the given condition else returns false.
Example :
object Elementtest
{
def main(args : Array[String])
{
val x = List( 21 , 20 , 33 , 40 , 27 , 62 )
val y = x count( _ < 40 )
println(y)
}
}
|
Here, count will display the number of elements of the collection which satisfies the given condition.
Last Updated :
15 Apr, 2019
Like Article
Save Article