Open In App

Scala Trait Traversable | Set-2

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: 




// 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

 




// 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)

 




// 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)

 




// 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)

 




// 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)

 




// 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)

 




// 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, ?)

 




// 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)

 




// 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

 




// 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

 




// 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

 




// 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

 




// 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)

 




// 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

 




// 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

 




// 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)

 




// 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

 


Article Tags :