prerequisite- Scala Trait Traversable | Set-1
In the previous Set we have seen some of the operations performed by the Class Taversable. Now, in this Set we will perceive some more operations.
These operations are as follows:
- Conversion operations:
The Conversion operations are toList, toSeq, toArray, toStream, toSet, toMap, toIterable, and toIndexedSeq. These operations changes the Collection of Traversable into a relatively distinct thing.
Example :
Scala
object Conversion
{
def main(args : Array[String])
{
val q = Set( 2 , 7 , 8 , 15 , 19 )
val r = q.toArray
println(r)
}
}
|
- Here, the Conversion operation i.e, toArray will convert the above Set (or any Traversable) into an Array.
Example :
Scala
object Conversion
{
def main(args : Array[String])
{
val q = Set( 2 , 6 , 3 , 7 , 15 , 20 )
val r = q.toList
println(r)
}
}
|
Output:
List(20, 6, 2, 7, 3, 15)
- Here, toList will convert any Collection of Traversable into a List.
Example :
Scala
object Conversion
{
def main(args : Array[String])
{
val x = List( 9 , 10 , 13 , 15 , 18 , 19 )
val y = x.toSet
println(y)
}
}
|
Output:
Set(10, 9, 13, 18, 19, 15)
- Here, the Conversion operation i.e, toSet will convert any collection of Traversable into a Set.
Example :
Scala
object Conversion
{
def main(args : Array[String])
{
val m = Set( 2 , 4 , 6 , 8 , 11 , 15 )
val n = m.toSeq
println(n)
}
}
|
Output:
ArrayBuffer(6, 2, 11, 8, 4, 15)
- Here, Conversion operation i.e, toSeq converts any Collection of Traversable into a Sequence. The sequence generated here is utilized in Vectors.
Example :
Scala
object Conversion
{
def main(args : Array[String])
{
val x = Set( 8 , 10 , 13 , 15 , 18 )
val y = x.toIterable
println(y)
}
}
|
Output:
Set(10, 13, 18, 8, 15)
- Here, the Conversion operation i.e, toIterable (It Iterates over all the elements of the collection) will convert any collection of a Traversable into an Iterable.
Example :
Scala
object Conversion
{
def main(args : Array[String])
{
val x = Set( 2 , 4 , 5 , 6 , 7 , 9 )
val y = x.toIndexedSeq
println(y)
}
}
|
Output:
Vector(5, 6, 9, 2, 7, 4)
- Here, the conversion operation i.e, toIndexedSeq converts any Traversable into an Indexed sequence. The Indexed sequence generated here is utilized in Strings and Vectors.
Example :
Scala
object Conversion
{
def main(args : Array[String])
{
val x = Set( 15 , 17 , 18 , 19 , 22 , 25 )
val y = x.toStream
println(y)
}
}
|
- Here, the Conversion operation i.e, toStream converts any collection of Traversable into a Stream. This Stream is enumerated lazily.
Example :
Scala
object Conversion
{
def main(args : Array[String])
{
val x = Set("GfG" -> "CS portal", "Nidhi" -> "a Geek")
val y = x.toMap
println(y)
}
}
|
Output:
Map(GfG -> CS portal, Nidhi -> a Geek)
- Here, toMap will convert any Traversable to a Map. A Set or a List must have parameters.
- Size info operations:
The Size info operations are nonEmpty, isEmpty, hasDefiniteSize, and size. These operations can specify if the given operation is finite or infinite.
Example :
Scala
object Sizeinfo
{
def main(args : Array[String])
{
val x = Map("gfg" -> "cs", "nidhi" -> "geek")
val y = x.isEmpty
println(y)
}
}
|
- Here, isEmpty checks if the Traversable collection is empty. If the collection of elements is empty then it prints true and if its not empty then it prints false.
Example :
Scala
object Sizeinfo
{
def main(args : Array[String])
{
val x = Map("gfg" -> "cs", "nidhi" -> "geek")
val y = x.nonEmpty
println(y)
}
}
|
- Here, nonEmpty checks if the Traversable collection contains elements. If there are elements in the collection then it displays true else false.
Example :
Scala
object Sizeinfo
{
def main(args : Array[String])
{
val q = Map("gfg" -> "cs", "nidhi" -> "geek",
"geeta" -> "coder")
val r = q.size
println(r)
}
}
|
- Here, size is utilized to evaluate the size of Traversable collection.
Example :
Scala
object Sizeinfo
{
def main(args : Array[String])
{
val q = Map("gfg" -> "cs", "nidhi" -> "geek",
"geeta" -> "coder")
val r = q.hasDefiniteSize
println(r)
}
}
|
- Here, hasDefiniteSize is utilized to check if the Traversable collection has finite elements or not. If the collection is finite then it returns true else false.
- Element retrieval operations:
The Element retrieval operations includes last, head, lastOption, headOption, and find. These operations are utilized to retrieve first or last element of the Traversable collection or to retrieve the first element corresponding to the given condition.
Example :
Scala
object Retrieval
{
def main(args : Array[String])
{
val x = Set( 12 , 13 , 14 , 15 )
val y = x.lastOption
println(y)
}
}
|
- Here, last element of the Traversable is returned by lastOption. The stated collection must be ordered, if there are no elements in the collection then None is returned.
Example :
Scala
object Retrieval
{
def main(args : Array[String])
{
val x = Set( 12 , 13 , 14 , 15 )
val y = x.last
println(y)
}
}
|
- Here, last will return the last element of the stated collection. The collection must be ordered, if its not ordered then some random element is returned.
Example :
Scala
object Retrieval
{
def main(args : Array[String])
{
val q = List( 12 , 24 , 36 , 48 )
val r = q.head
println(r)
}
}
|
- Here, head will return first element of the Traversable collection if its ordered and if the collection is not ordered then any random element is returned.
Example:
Scala
object Retrieval
{
def main(args : Array[String])
{
val x = List( 8 , 10 , 21 , 17 , 29 )
val y = x.find( _ % 3 == 0 )
println(y)
}
}
|
- Here, find will retrieve the first element of the collection, which matches the stated condition.
Example :
Scala
object Retrieval
{
def main(args : Array[String])
{
val p = List( 7 , 9 , 11 , 19 , 21 )
val q = List()
val r = p.headOption
val s = q.headOption
println(r)
println(s)
}
}
|
- Here, headOption returns first element of an ordered collection but returns None if the collection is empty.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
24 Jan, 2022
Like Article
Save Article