Open In App
Related Articles

Scala Iterator buffered() method with example

Improve Article
Improve
Save Article
Save
Like Article
Like

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 :




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


Output:

non-empty iterator

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




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


Output:

empty iterator

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


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 May, 2019
Like Article
Save Article
Previous
Next
Similar Reads