Scala Iterator hasDefiniteSize() method with example

The hasDefiniteSize() method is a concrete value member of AbstractMap class. It checks whether the stated traversable collection is having a finite size or not.

  • Method Definition:

    def hasDefiniteSize: Boolean
    
  • Return Type:
    It returns true if the stated collection has finite size else returns false or if the iterator is empty it returns true otherwise returns false.

Example :



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

true

Here, the iterator is empty so, the hasDefiniteSize method returns true.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of hasDefiniteSize()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Declaring an iterator
        val iter = Iterator(2, 3, 4, 9, 6)
          
        // Applying hasDefiniteSize method
        val result = iter.hasDefiniteSize
          
        // Displays output
        println(result)
  
    }

chevron_right


Output:

false

Here, the stated iterator is non-empty so, the method hasDefiniteSize returns false.



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.