Scala Iterator length() method with example

The length() method belongs to the concrete value members of the class AbstractIterator. It is defined in the class Iterator. It is also identical to the method size, it is also utilized to find the length of the stated collection.

Method Definition : final def length: Int

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



Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of length()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Declaring an iterator
        val iter = Iterator(2, 3, 5, 7, 11)
          
        // Applying length method
        val iter1 = iter.length
          
        // Displays output
        println(iter1)
  
    }
}

chevron_right


Output:

5

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

0

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



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.