Scala Iterator size() method with example

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:



5

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

0

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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.