Open In App

Scala Iterator size() method with example

Last Updated : 30 Jun, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The size() method belongs to the concrete value members of the class Abstract Iterator. It is defined in the classes IterableOnceOps. It is utilized to find the size of the stated collection. It will not terminate for the infinite sized collection.

Method Definition : def size: Int

Return Type :It returns the size of the stated collection..

Example #1:




// Scala program of size()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Declaring an iterator
        val iter = Iterator(2.2, 3.6, 6.6, 9.9, 2.1)
          
        // Applying size method
        val iter1 = iter.size
          
        // Displays output
        println(iter1)
  
    }
}


Output:

5

Here, the length of the iterator is returned as the stated collection is non-empty.
Example #2:




// Scala program of size()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Declaring an empty-iterator
        val iter = Iterator()
          
        // Applying size method
        val iter1 = iter.size
          
        // Displays output
        println(iter1)
  
    }
}


Output:

0

Here, the length of the iterator is zero as the stated collection is empty.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads