Skip to content
Related Articles

Related Articles

Scala Trait Traversable | Set-2

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 24 Jan, 2022

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




// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating Set of numbers
        val q = Set(2, 7, 8, 15, 19)
 
        // Converting Set to an Array
        val r = q.toArray
 
        // Displaying an Array
        println(r)
    }
}

Output: 

[I@506e1b77

 

  • Here, the Conversion operation i.e, toArray will convert the above Set (or any Traversable) into an Array. 
    Example : 

Scala




// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating Set of numbers
        val q = Set(2, 6, 3, 7, 15, 20)
 
        // Converting a Set to a List
        val r = q.toList
 
        // Displaying a List
        println(r)
         
    }
}

Output: 

List(20, 6, 2, 7, 3, 15)

 

  • Here, toList will convert any Collection of Traversable into a List. 
    Example : 

Scala




// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating List of numbers
        val x = List(9, 10, 13, 15, 18, 19)
 
        // Converting a List to a Set
        val y = x.toSet
 
        // Displaying a Set
        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




// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating Set of numbers
        val m = Set(2, 4, 6, 8, 11, 15)
 
        // Converting a Set to a Sequence
        val n = m.toSeq
 
        // Displaying a Sequence
        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




// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating Set of numbers
        val x = Set(8, 10, 13, 15, 18)
 
        // Converting a Set to an Iterable
        val y = x.toIterable
 
        // Displaying an iterable
        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




// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating Set of numbers
        val x = Set(2, 4, 5, 6, 7, 9)
 
        // Converting a Set to an
        // Indexed sequence
        val y = x.toIndexedSeq
 
        // Displaying an Indexed sequence
        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




// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating Set of numbers
        val x = Set(15, 17, 18, 19, 22, 25)
 
        // Converting a Set to a stream
        val y = x.toStream
 
        // Displaying a Stream
        println(y)
    }
}

Output: 

Stream(25, ?)

 

  • Here, the Conversion operation i.e, toStream converts any collection of Traversable into a Stream. This Stream is enumerated lazily. 
    Example : 

Scala




// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a Set of parameters
        val x = Set("GfG" -> "CS portal", "Nidhi" -> "a Geek")
 
        // Converting a Set to a Map
        val y = x.toMap
 
        // Displaying a Map
        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




// Scala program of Size info operation
 
// Creating object
object Sizeinfo
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a map
        val x = Map("gfg" -> "cs", "nidhi" -> "geek")
 
        // Applying Size info operation
        val y = x.isEmpty
 
        // Displays true if map is
        // empty
        println(y)
    }
}

Output: 

false

 

  • 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




// Scala program of Size info operation
 
// Creating object
object Sizeinfo
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a map
        val x = Map("gfg" -> "cs", "nidhi" -> "geek")
 
        // Applying Size info operation
        val y = x.nonEmpty
 
        // Displays true if map is
        // not empty
        println(y)
    }
}

Output: 

true

 

  • Here, nonEmpty checks if the Traversable collection contains elements. If there are elements in the collection then it displays true else false. 
    Example : 

Scala




// Scala program of Size info operation
 
// Creating object
object Sizeinfo
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a map
        val q = Map("gfg" -> "cs", "nidhi" -> "geek",
                                    "geeta" -> "coder")
 
        // Applying Size info operation
        val r = q.size
 
        // Displays size of the Map
        println(r)
    }
}

Output: 

3

 

  • Here, size is utilized to evaluate the size of Traversable collection. 
    Example : 

Scala




// Scala program of Size info operation
 
// Creating object
object Sizeinfo
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a map
        val q = Map("gfg" -> "cs", "nidhi" -> "geek",
                                    "geeta" -> "coder")
 
        // Applying Size info operation
        val r = q.hasDefiniteSize
 
        // Displays true if number of
        // elements in Map are finite
        println(r)
    }
}

Output: 

true

 

  • 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




// Scala program of Element
// retrieval operation
 
// Creating object
object Retrieval
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a Set of numbers
        val x = Set(12, 13, 14, 15)
 
        // Applying element retrieval
        // operation
        val y = x.lastOption
 
        // Displays last element
        // of the Set
        println(y)
    }
}

Output: 

Some(15)

 

  • 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




// Scala program of Element
// retrieval operation
 
// Creating object
object Retrieval
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a Set of numbers
        val x = Set(12, 13, 14, 15)
 
        // Applying element retrieval
        // operation
        val y = x.last
 
        // Displays last element
        // of the Set
        println(y)
    }
}

Output: 

15

 

  • 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




// Scala program of Element
// retrieval operation
 
// Creating object
object Retrieval
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a List of numbers
        val q = List(12, 24, 36, 48)
 
        // Applying element retrieval
        // operation
        val r = q.head
 
        // Displays first element
        // of the List
        println(r)
    }
}

Output: 

12

 

  • 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




// Scala program of Element
// retrieval operation
 
// Creating object
object Retrieval
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a List of numbers
        val x = List(8, 10, 21, 17, 29)
 
        // Applying element retrieval
        // operation
        val y = x.find(_ % 3 == 0)
 
        // Displays first element
        // matching the condition
        println(y)
 
    }
}

Output: 

Some(21)

 

  • Here, find will retrieve the first element of the collection, which matches the stated condition. 
    Example : 

Scala




// Scala program of Element
// retrieval operation
 
// Creating object
object Retrieval
{
 
    // Main method
    def main(args: Array[String])
    {
     
        // Creating List of numbers
        val p = List(7, 9, 11, 19, 21)
 
        // Creating a empty List
        val q = List()
 
        // Applying element retrieval
        // operation
        val r = p.headOption
        val s = q.headOption
 
        // Displays first element
        // if the List is not empty
        println(r)
        println(s)
    }
}

Output: 

Some(7)
None

 

  • Here, headOption returns first element of an ordered collection but returns None if the collection is empty. 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!