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 program of Conversion operation// Creating objectobjectConversion{// Main methoddefmain(args:Array[String]){// Creating Set of numbersvalq=Set(2,7,8,15,19)// Converting Set to an Arrayvalr=q.toArray// Displaying an Arrayprintln(r)}}chevron_rightfilter_noneOutput:[I@506e1b77
Here, the Conversion operation i.e, toArray will convert the above Set (or any Traversable) into an Array.
Example :
// Scala program of Conversion operation// Creating objectobjectConversion{// Main methoddefmain(args:Array[String]){// Creating Set of numbersvalq=Set(2,6,3,7,15,20)// Converting a Set to a Listvalr=q.toList// Displaying a Listprintln(r)}}chevron_rightfilter_noneOutput:List(20, 6, 2, 7, 3, 15)
Here, toList will convert any Collection of Traversable into a List.
Example :// Scala program of Conversion operation// Creating objectobjectConversion{// Main methoddefmain(args:Array[String]){// Creating List of numbersvalx=List(9,10,13,15,18,19)// Converting a List to a Setvaly=x.toSet// Displaying a Setprintln(y)}}chevron_rightfilter_noneOutput: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 program of Conversion operation// Creating objectobjectConversion{// Main methoddefmain(args:Array[String]){// Creating Set of numbersvalm=Set(2,4,6,8,11,15)// Converting a Set to a Sequencevaln=m.toSeq// Displaying a Sequenceprintln(n)}}chevron_rightfilter_noneOutput: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 program of Conversion operation// Creating objectobjectConversion{// Main methoddefmain(args:Array[String]){// Creating Set of numbersvalx=Set(8,10,13,15,18)// Converting a Set to an Iterablevaly=x.toIterable// Displaying an iterableprintln(y)}}chevron_rightfilter_noneOutput: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 program of Conversion operation// Creating objectobjectConversion{// Main methoddefmain(args:Array[String]){// Creating Set of numbersvalx=Set(2,4,5,6,7,9)// Convrerting a Set to an// Indexed sequencevaly=x.toIndexedSeq// Displaying an Indexed sequenceprintln(y)}}chevron_rightfilter_noneOutput: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 program of Conversion operation// Creating objectobjectConversion{// Main methoddefmain(args:Array[String]){// Creating Set of numbersvalx=Set(15,17,18,19,22,25)// Converting a Set to a streamvaly=x.toStream// Displaying a Streamprintln(y)}}chevron_rightfilter_noneOutput:Stream(25, ?)
Here, the Conversion operation i.e, toStream converts any collection of Traversable into a Stream. This Stream is enumerated lazily.
Example :// Scala program of Conversion operation// Creating objectobjectConversion{// Main methoddefmain(args:Array[String]){// Creating a Set of parametersvalx=Set("GfG"->"CS portal","Nidhi"->"a Geek")// Converting a Set to a Mapvaly=x.toMap// Displaying a Mapprintln(y)}}chevron_rightfilter_noneOutput: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 program of Size info operation// Creating objectobjectSizeinfo{// Main methoddefmain(args:Array[String]){// Creating a mapvalx=Map("gfg"->"cs","nidhi"->"geek")// Applying Size info operationvaly=x.isEmpty// Displays true if map is// emptyprintln(y)}}chevron_rightfilter_noneOutput: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 program of Size info operation// Creating objectobjectSizeinfo{// Main methoddefmain(args:Array[String]){// Creating a mapvalx=Map("gfg"->"cs","nidhi"->"geek")// Applying Size info operationvaly=x.nonEmpty// Displays true if map is// not emptyprintln(y)}}chevron_rightfilter_noneOutput: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 program of Size info operation// Creating objectobjectSizeinfo{// Main methoddefmain(args:Array[String]){// Creating a mapvalq=Map("gfg"->"cs","nidhi"->"geek","geeta"->"coder")// Applying Size info operationvalr=q.size// Displays size of the Mapprintln(r)}}chevron_rightfilter_noneOutput:3
Here, size is utilized to evaluate the size of Traversable collection.
Example :// Scala program of Size info operation// Creating objectobjectSizeinfo{// Main methoddefmain(args:Array[String]){// Creating a mapvalq=Map("gfg"->"cs","nidhi"->"geek","geeta"->"coder")// Applying Size info operationvalr=q.hasDefiniteSize// Displays true if number of// elements in Map are finiteprintln(r)}}chevron_rightfilter_noneOutput: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 program of Element// retrieval operation// Creating objectobjectRetrieval{// Main methoddefmain(args:Array[String]){// Creating a Set of numbersvalx=Set(12,13,14,15)// Applying element retrieval// operationvaly=x.lastOption// Displays last element// of the Setprintln(y)}}chevron_rightfilter_noneOutput: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 program of Element// retrieval operation// Creating objectobjectRetrieval{// Main methoddefmain(args:Array[String]){// Creating a Set of numbersvalx=Set(12,13,14,15)// Applying element retrieval// operationvaly=x.last// Displays last element// of the Setprintln(y)}}chevron_rightfilter_noneOutput: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 program of Element// retrieval operation// Creating objectobjectRetrieval{// Main methoddefmain(args:Array[String]){// Creating a List of numbersvalq=List(12,24,36,48)// Applying element retrieval// operationvalr=q.head// Displays first element// of the Listprintln(r)}}chevron_rightfilter_noneOutput: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 program of Element// retrieval operation// Creating objectobjectRetrieval{// Main methoddefmain(args:Array[String]){// Creating a List of numbersvalx=List(8,10,21,17,29)// Applying element retrieval// operationvaly=x.find(_%3==0)// Displays first element// matching the conditionprintln(y)}}chevron_rightfilter_noneOutput:Some(21)
Here, find will retrieve the first element of the collection, which matches the stated condition.
Example :// Scala program of Element// retrieval operation// Creating objectobjectRetrieval{// Main methoddefmain(args:Array[String]){// Creating List of numbersvalp=List(7,9,11,19,21)// Creating a empty Listvalq=List()// Applying element retrieval// operationvalr=p.headOptionvals=q.headOption// Displays first element// if the List is not emptyprintln(r)println(s)}}chevron_rightfilter_noneOutput:Some(7) None
Here, headOption returns first element of an ordered collection but returns None if the collection is empty.
Recommended Posts:
- Scala Trait Traversable | Set-1
- Scala Trait Traversable | Set-3
- Scala Trait Traversable | Set-4
- Scala | Sealed Trait
- Scala | Trait App
- Scala | Trait Mixins
- Trait Linearization in Scala
- Program to convert Java Set to a Traversable in Scala
- Program to convert Java list to Traversable in Scala
- Program to convert Java Set of strings to a Traversable in Scala
- Program to convert Java Set of characters to a Traversable in Scala
- Program to convert Java list of Strings to Traversable in Scala
- Program to convert Java list of character to Traversable in Scala
- Program to convert Java list of floats to Traversable in Scala
- Program to convert Java list of doubles to Traversable in Scala
- Program to convert Java list of Shorts to Traversable in Scala
- Program to convert Java list of bytes to Traversable in Scala
- Program to convert Java Set of floats to a Traversable in Scala
- Program to convert Java Set of doubles to a Traversable in Scala
- Program to convert Java Set of Shorts to a Traversable in Scala
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

