Introduction:
A root trait of the entire class of the Scala collections is Trait Traversable. It is available at the uppermost position of the collection hierarchy. It has uniquely one abstract operation, which is foreach. Here each of the operations are assured to be executed in a single-threaded approach.
Syntax:
def foreach[U](f: Elem => U)
Here, the operation f is concerned with all the elements of the collection and Elem => U is the type of operation, where “Elem” is the type of elements in the Scala’s collection and “U” is an inconsistent result type.
Some Important points:
- Traversable is accomplished by the Scala’s Collection classes.
- It is obligatory for Traversable to define foreach method only, as Traversable can inherit all the other methods.
- The foreach method could traverse all the elements of the Scala’s collection class.
- There are numerous concrete methods which are defined by Traversables.
- List, Array, Map, Set, and many more are the subclass of Traversables.
- Abstract Method:
The only abstract method here is foreach, which can traverse over all the elements of the Collection.
Example ://Scala program of abstract method//Creating objectobjectCS{//Main methoddefmain(args:Array[String]){//Creating an Array of elementsvalx=Array("GEEKS","FOR","GEEKS")//Calling foreach methodx.foreach { E=>valy=E.toLowerCase//Display stringsprintln(y)}}}chevron_rightfilter_noneOutput:
geeks for geeks
Here, all the elements of the Array stated above are traversed by foreach method and then they are converted from uppercase to lowercase.
Example:// Scala program of abstract method// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){//Creating list of numbersvall=List(2,6,8,7,10,11,13)//Calling foreach and displaying//numbers of list after//multiplication each of theml.foreach(n=>println(n *6))}}chevron_rightfilter_noneOutput:12 36 48 42 60 66 78
Here, foreach method traverses all the numbers of the list and multiplies each of them.
- Addition operation:
Here, Addition operation i.e, ++ adds two Traversables together or adds each of the elements of an iterator to a Traversable.
Example:// Scala program of addition operation// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating set of numbersvalx=Set(7,8,9,10)// Creating list of numbersvaly=List(1,5,8,18)//performing addition operationvals1=x ++ yvals2=y ++ x//Displaying setprintln(s1)//Displaying listprintln(s2)}}chevron_rightfilter_noneOutput:Set(5, 10, 1, 9, 7, 18, 8) List(1, 5, 8, 18, 7, 8, 9, 10)
Here, when a Set is added to a List then a Set is generated and when a List is added to a Set then a List is generated.
- Map operations:
The three Map operations are map, flatMap, and collect.
These Map operations create a new collection by assigning a few function to the elements of Scala collection.
Example :// Scala program of Map operations// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating Set of numbersvalx=Set(8,9,5,10)// Applying map operationvaly=x.map(_*9)// Displaying Setprintln(y)}}chevron_rightfilter_noneOutput:Set(72, 81, 45, 90)
Here, the Map operation (i.e, map) will assign the stated function on each elements of a Traversable and will return a new collection as output.
Example :// Scala program of Map operations// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating a List of listsvalq=List(List(7), List(8,9,10), List(11,12,13))// Applying map operationvalr=q.flatMap(_.map(_+3))// Displaying Listprintln(r)}}chevron_rightfilter_noneOutput:List(10, 11, 12, 13, 14, 15, 16)
Here, the flatMap will assign the stated function on each elements within the elements and then will Concatenate the output.
Example :// Scala program of Map operations// Creating objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating List of numbersvalx=List(9,3,5,11,15,4)// Applying map operationvaly=x.collect{// Partial functioncasez:Intif(z%3==0)=>z +2}//Displaying listprintln(y)}}chevron_rightfilter_noneOutput:List(11, 5, 17)
Here, collect will assign a partial function to each elements of Traversable and will give a non-identical collection as output.
Recommended Posts:
- Scala Trait Traversable | Set-2
- Scala Trait Traversable | Set-3
- Scala Trait Traversable | Set-4
- Scala | Trait App
- Scala | Trait Mixins
- Trait Linearization in Scala
- Scala | Sealed Trait
- Program to convert Java Set to a Traversable in Scala
- Program to convert Java Set of Shorts to a Traversable in Scala
- Program to convert Java list to Traversable in Scala
- Program to convert Java Set of characters to a 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 strings to a Traversable in Scala
- Program to convert Java Set of bytes to a Traversable in Scala
- Program to convert Java list of Shorts to Traversable in Scala
- Program to convert Java list of doubles to Traversable in Scala
- Program to convert Java list of Strings to Traversable in Scala
- Program to convert Java list of bytes to Traversable in Scala
- Program to convert Java list of floats to 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.

