Scala Iterators
[ Scala Collections](#)\n\nScala Iterator is not a collection; it is a method for accessing collections.\n\nThe two basic operations of an iterator `it` are **next** and **hasNext**.\n\nCalling **it.next()** returns the next element of the iterator and updates the state of the iterator.\n\nCalling **it.hasNext()** is used to detect whether there are still elements in the collection.\n\nThe simplest way to make the iterator `it` return all elements one by one is to use a while loop:\n\n## Example\n\nobject Test {\n\ndef main(args: Array){\n\nval it = Iterator("Baidu", "Google", "Tutorial", "Taobao")\n\nwhile(it.hasNext){\n\nprintln(it.next())\n\n}\n\n}\n\n}\n\nExecuting the above code, the output result is:\n\n$ scalac Test.scala $ scala TestBaiduGoogleTutorialTaobao\n\n* * *\n\n## Finding Maximum and Minimum Elements\n\nYou can use the **it.min** and **it.max** methods to find the minimum and maximum elements from the iterator. An example is as follows:\n\n## Example\n\nobject Test {\n\ndef main(args: Array){\n\nval ita = Iterator(20,40,2,50,69, 90)\n\nval itb = Iterator(20,40,2,50,69, 90)\n\nprintln("The maximum element is:" + ita.max)\n\n println("The minimum element is:" + itb.min)\n\n}\n\n}\n\nExecuting the above code, the output result is:\n\n$ scalac Test.scala $ scala TestThe maximum element is:90The minimum element is:2\n\n* * *\n\n## Getting the Length of an Iterator\n\nYou can use the **it.size** or **it.length** methods to view the number of elements in the iterator. An example is as follows:\n\n## Example\n\nobject Test {\n\ndef main(args: Array){\n\nval ita = Iterator(20,40,2,50,69, 90)\n\nval itb = Iterator(20,40,2,50,69, 90)\n\nprintln("ita.size value of: " + ita.size)\n\n println("itb.length value of: " + itb.length)\n\n}\n\n}\n\nExecuting the above code, the output result is:\n\n$ scalac Test.scala $ scala Test ita.size value of: 6 itb.length value of: 6\n\n* * *\n\n## Common Methods of Scala Iterator\n\nThe following table lists the commonly used methods of Scala Iterator:\n\n| No. | Method & Description |\n| --- | --- |\n| 1 | **def hasNext: Boolean** Returns true if there are still elements that can be returned. |\n| 2 | **def next(): A** Returns the next element of the iterator and updates the state of the iterator |\n| 3 | **def ++(that: => Iterator): Iterator** Merges two iterators |\n| 4 | **def ++[B >: A](that :=> GenTraversableOnce): Iterator** Merges two iterators |\n| 5 | **def addString(b: StringBuilder): StringBuilder** Appends a string to StringBuilder b |\n| 6 | **def addString(b: StringBuilder, sep: String): StringBuilder** Appends a string to StringBuilder b, with a specified separator |\n| 7 | **def buffered: BufferedIterator** Converts the iterator into a BufferedIterator |\n| 8 | **def contains(elem: Any): Boolean** Checks whether the iterator contains the specified element |\n| 9 | **def copyToArray(xs: Array, start: Int, len: Int): Unit** Copies selected values from the iterator to an array |\n| 10 | **def count(p: (A) => Boolean): Int** Returns the total number of elements in the iterator that satisfy the condition p. |\n| 11 | **def drop(n: Int): Iterator** Returns a new collection discarding the first n elements |\n| 12 | **def dropWhile(p: (A) => Boolean): Iterator** Drops elements from left to right until the condition p is not met |\n| 13 | **def duplicate: (Iterator, Iterator)** Generates two iterators that can each return all elements of the iterator. |\n| 14 | **def exists(p: (A) => Boolean): Boolean** Returns a boolean value indicating whether there is an element in the iterator that satisfies p. |\n| 15 | **def filter(p: (A) => Boolean): Iterator** Returns a new iterator pointing to all elements in the iterator that satisfy the condition p. |\n| 16 | **def filterNot(p: (A) => Boolean): Iterator** Returns an iterator pointing to elements in the iterator that do not satisfy the condition p. |\n| 17 | **def find(p: (A) => Boolean): Option** Returns the first element that satisfies p, or None. Note: If an element satisfying the condition is found, the iterator will be positioned after that element; if not found, it will be positioned at the end. |\n| 18 | **def flatMap(f: (A) => GenTraversableOnce): Iterator** Applies function f to each element in the iterator's sequence and returns an iterator pointing to the resulting sequence. |\n| 19 | **def forall(p: (A) => Boolean): Boolean** Returns a boolean value indicating whether all elements pointed to by it satisfy p. |\n| 20 | **def foreach(f: (A) => Unit): Unit** Executes the specified program f on each element returned by the iterator |\n| 21 | **def hasDefiniteSize: Boolean** Returns true if the number of elements in the iterator is finite (defaults to isEmpty) |\n| 22 | **def indexOf(elem: B): Int** Returns the first element in the iterator whose index equals x. Note: The iterator will pass this element. |\n| 23 | **def indexWhere(p: (A) => Boolean): Int** Returns the element in the iterator whose index satisfies the condition p. Note: The iterator will pass this element. |\n| 24 | **def isEmpty: Boolean** Checks if it is empty, returns true if empty, otherwise returns false (opposite of hasNext). |\n| 25 | **def isTraversableAgain: Boolean** Tests whether this Iterator can be repeatedly traversed. |\n| 26 | **def length: Int** Returns the number of elements in the iterator. |\n| 27 | **def map(f: (A) => B): Iterator** Generates a new iterator from the results of passing each element in it through function f. |\n| 28 | **def max: A** Returns the largest element among the iterator's elements. |\n| 29 | **def min: A** Returns the smallest element among the iterator's elements. |\n| 30 | **def mkString: String** Converts all elements of the iterator into a string. |\n| 31 | **def mkString(sep: String): String** Converts all elements of the iterator into a string, with a specified separator. |\n| 32 | **def nonEmpty: Boolean** Checks whether the container contains elements (equivalent to hasNext). |\n| 33 | **def padTo(len: Int, elem: A): Iterator** First returns all elements of the iterator, appending copies of elem until the length reaches len. |\n| 34 | **def patch(from: Int, patchElems: Iterator, replaced: Int): Iterator** Returns a new iterator where `replaced` elements starting from the `from`-th element are replaced by the elements pointed to by the patchElems iterator. |\n| 35 | **def product: A** Returns the product of the numeric elements pointed to by the iterator. |\n| 36 | **def sameElements(that: Iterator): Boolean** Determines whether the iterator and the specified iterator parameter sequentially return the same elements |\n| 37 | **def seq: Iterator** Returns a sequential view of the collection |\n| 38 | **def size: Int** Returns the number of elements in the iterator |\n| 39 | **def slice(from: Int, until: Int): Iterator** Returns a new iterator pointing to a segment of the sequence pointed to by the iterator, starting from the `from`-th element and ending at the `until`-th element. |\n| 40 | **def sum: A** Returns the sum of the numeric elements pointed to by the iterator |\n| 41 | **def take(n: Int): Iterator** Returns a new iterator of the first n elements. |\n| 42 | **def toArray: Array** Places all elements pointed to by the iterator into an array and returns it. |\n| 43 | **def toBuffer: Buffer** Copies all elements pointed to by the iterator into a Buffer. |\n| 44 | **def toIterable: Iterable** Returns an Iterable containing all elements of this traversable or iterator. This will not terminate for infinite iterators. |\n| 45 | **def toIterator: Iterator** Places all elements of the iterator into an Iterator container and returns it. |\n| 46 | **def toList: List** Places all elements of the iterator into a List and returns it |\n| 47 | **def toMap[T, U]: Map[T, U]** Places all key-value pairs of the iterator into a Map and returns it. |\n| 48 | **def toSeq: Seq** Places all elements of the iterator into a Seq container and returns it. |\n| 49 | **def toString(): String** Converts the iterator to a string |\n| 50 | **def zip(that: Iterator): Iterator[(A, B)** Returns a new iterator pointing
YouTip