Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Scala Iterator seq() method with example

  • Last Updated : 28 May, 2019

The seq() method belongs to the concrete value members of the class Iterable. It is helpful in visualizing the sequential view of the stated collection. It has a time complexity of O(1).

  • Method Definition:
    def seq: Iterator[A]
    
  • Return Type:
    It returns a sequential view of the iterator.

Example :




// Scala program of seq()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Declaring an iterator
        val iter = Iterator(3, 4, 5, 6, 7)
          
        // Applying seq method in 
        // for loop
        for(n<-iter.seq)
        {
              
            // Displays output
            println(n)
  
        }
    }
}
Output:
3
4
5
6
7

Here, a sequential view of the stated iterator is returned by the seq method of Scala.
Example :




// Scala program of seq()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Declaring an iterator
        val iter = Iterator(8, 9, 10, 11)
          
        // Applying seq method
        val result = iter.seq
              
        // Displays output
        println(result)
  
    }
}
Output:
non-empty iterator

Here, a non-empty iterator is returned.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!