VOOZH about

URL: https://www.geeksforgeeks.org/scala/scala-trait-traversable-set-2/

⇱ Scala Trait Traversable | Set-2 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Scala Trait Traversable | Set-2

Last Updated : 11 Jul, 2025

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 : 

Output: 
[I@506e1b77

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

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

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

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 : 

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 : 

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 : 

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 : 

Output: 
Stream(25, ?)

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

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 : 

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 : 

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 : 

Output: 
3

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

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 : 

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 : 

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 : 

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: 

Output: 
Some(21)

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

Output: 
Some(7)
None

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

Explore