Scala Iterator slice() method with example Last Updated : 06 Jun, 2019 Comments Improve Suggest changes Like Article Like Report The slice() method belongs to the concrete value members of the class AbstractIterator. It is defined in the class Iterator. It creates a new iterator for the interval given in the slice. The first value present in the slice indicates the start of the element in the new iterator and the second value present in the slice indicates the end. Method Definition: def slice(from: Int, until: Int): Iterator[A] Where, from implies index of the first element and until implies index of the first element following the slice. Return Type: It returns a new iterator with elements from from until until. Example : Scala // Scala program of slice() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Declaring a iterator val iter = Iterator(1, 2, 3, 4, 5) // Applying slice method val iter1 = iter.slice(1, 4) // Using while loop to print the // elements of new iterator while(iter1.hasNext) { // Displays output println(iter1.next()) } } } Output: 2 3 4 Here, if the interval present in the slice is like (n, m) then the elements will be printed from the nth index till (m-1)th index. The functions hasNext and next are used here to print the elements of the new iterator. Example : Scala // Scala program of slice() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Declaring a iterator val iter = Iterator(2, 4, 5, 6) // Applying slice method val iter1 = iter.slice(0, 3) // Using while loop to print the // elements of new iterator while(iter1.hasNext) { // Displays output println(iter1.next()) } } } Output: 2 4 5 Here, the elements are printed from the index zero till the second index. Comment More infoAdvertise with us Next Article Scala Iterator slice() method with example N nidhi1352singh Follow Improve Article Tags : Scala Scala Scala-Method Similar Reads Scala Iterator seq() method with example 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 : Scal 1 min read Scala Iterator toSeq() method with example The toSeq() method belongs to the concrete value member of the class Abstract Iterator. It is equivalent to the Seq method but this method is more faster. Method Definition: val result = iter.toSeq Return Type: It returns the stated collection as a sequence. Example #1: Scala // Scala program of toS 1 min read Scala Iterator take() method with example The take() method belongs to the concrete value member of the class Abstract Iterator. It is utilized to select the first n elements of the stated iterator. Method Definition: def take(n: Int): Iterator[A] Where, n is the number of element to take from the given iterator. Return Type: It returns the 2 min read Scala SortedMap iterator method with example The iterator method is utilized to give an iterator. Method Definition: def iterator: Iterator[(A, B)] Return Type: It returns a non-empty iterator for non-empty SortedMap and returns an empty iterator for empty SortedMap. Example #1: Scala // Scala program of iterator // method import scala.collect 1 min read Scala Map iterator method with example The iterator method is utilized to give an iterator. Method Definition: def iterator: Iterator[(A, B)] Return Type: It returns a non-empty iterator for non-empty map and returns an empty iterator for empty map. Example #1: Scala // Scala program of iterator // method // Creating object object GfG { 1 min read Like