Scala Iterator buffered() method with example

The buffered() method in Scala belongs to the concrete value members of the class iterator of Scala. It creates a buffered iterator from the iterator stated.

  • Method Definition:

     def buffered: BufferedIterator[A]
    
  • Return Type:
    It returns a buffered iterator which produces the alike values as the stated iterator.

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

non-empty iterator

Here, a buffered iterator of the stated iterator is returned after applying buffered method.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

empty iterator

Here, the stated iterator is empty so, an empty buffered iterator is returned after applying buffered method.




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.