Related Articles

Related Articles

Scala Iterator size() method with example
  • Last Updated : 30 Jun, 2019

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
Recommended Articles
Page :