Scala Iterator take() method with example Last Updated : 13 Aug, 2019 Comments Improve Suggest changes Like Article Like Report 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 first n values from the stated iterator, or the whole iterator, whichever is shorter. Example #1: Scala // Scala program of take() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a Iterator val iter = Iterator(2, 3, 5, 7, 8, 9) // Applying take method val iter1 = iter.take(4) // Applying while loop and // hasNext() method while(iter1.hasNext) { // Applying next() method and // displaying output println(iter1.next()) } } } Output: 2 3 5 7 Here, first four elements are displayed as we have selected first four elements in the method. Example #2: Scala // Scala program of take() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a Iterator val iter = Iterator(2, 3, 5, 7, 8, 9) // Applying take method val iter1 = iter.take(7) // Applying while loop and // hasNext() method while(iter1.hasNext) { // Applying next() method and // displaying output println(iter1.next()) } } } Output: 2 3 5 7 8 9 Here, the whole iterator is displayed as the number of elements in that is shorter than the number of elements selected by the method. so, the shorter one is displayed. Comment More infoAdvertise with us Next Article Scala Iterator take() 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 toSet() method with example The toSet() method belongs to the concrete value member of the class Abstract Iterator. It is defined in the class IterableOnceOps. Method Definition: def toSet[B >: A]: immutable.Set[B] Return Type: It returns a set from the stated collection. Example #1: Scala // Scala program of toSet() // met 1 min read Scala Iterator size() method with example The size() method belongs to the concrete value members of the class Abstract Iterator. It is defined in the classes IterableOnceOps. It is utilized to find the size of the stated collection. It will not terminate for the infinite sized collection. Method Definition : def size: Int Return Type :It r 1 min read Scala Iterator slice() method with example 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 2 min read Like