Scala Iterator
Scala Collection
Scala Iterator is not a collection, it is a method for accessing a collection.
The two basic operations of iterator it are next and hasNext.
Calling it.next() will return the next element of the iterator and update the state of the iterator.
Call it.hasNext() is used to detect whether there are any elements in the collection.
The easiest way to let the iterator it return all elements one by one is to use a while loop:
object Test { def main(args: Array[String]) { val it = Iterator("Baidu", "Google", "php", "Taobao") while (it.hasNext){ println(it.next()) } } }
Execute the above code, the output result is:
$ scalac Test.scala $ scala Test Baidu Google php Taobao
Find the maximum With the minimum element
you can use the it.min and it.max methods to find the maximum and minimum elements from the iterator, the example is as follows:
object Test { def main(args: Array[String]) { val ita = Iterator(20,40,2,50,69, 90) val itb = Iterator(20,40,2,50,69, 90) println("最大元素是:" + ita.max ) println("最小元素是:" + itb.min ) } }
Execute the above code, the output result is:
$ scalac Test.scala $ scala Test 最大元素是:90 最小元素是:2
Get the length of the iterator
You can use it.size or it.length Method to view the number of elements in the iterator. The example is as follows:
object Test { def main(args: Array[String]) { val ita = Iterator(20,40,2,50,69, 90) val itb = Iterator(20,40,2,50,69, 90) println("ita.size 的值: " + ita.size ) println("itb.length 的值: " + itb.length ) } }
Execute the above code, the output result is:
$ scalac Test.scala $ scala Test ita.size 的值: 6 itb.length 的值: 6
Commonly used methods of Scala Iterator
The following table lists the commonly used methods of Scala Iterator:
Serial number | Method and description |
---|---|
1 | def hasNext: Boolean If there are returnable elements, return true. |
2 | def next(): A Returns the next element of the iterator, And update the status of the iterator |
3 | def ++(that: => Iterator[A]): Iterator [A] Merge two iterators |
4 | def ++[B >: A](that :=> GenTraversableOnce[B]): Iterator[B] Merge two iterators |
5 | ##def addString(b: StringBuilder): StringBuilder Add a string to StringBuilder b |
##def addString(b: StringBuilder, sep: String): StringBuilder Add a string to StringBuilder b and specify the separator | |
def buffered: BufferedIterator[A] Iterators are converted to BufferedIterator | |
def contains(elem: Any): Boolean Detect whether the iterator Contains the specified element | |
def copyToArray(xs: Array[A], start: Int, len: Int): Unit Pass the selected value in the iterator to the array | |
def count(p: (A) => Boolean): Int Returns the total number of elements in the iterator that satisfy condition p. | |
def drop(n: Int): Iterator[A] return Discard the first n elements of the new set | |
12 | ##def dropWhile(p: (A) => Boolean): Iterator[A] Discard elements from left to right until condition p is not true |
def duplicate: (Iterator[A], Iterator[A]) Generate two iterators that can return all elements of the iterator respectively. | |
def exists(p: (A) => Boolean): Boolean Returns a Boolean value indicating whether there is an element satisfying p in the iterator element. | |
def filter(p: (A) => Boolean): Iterator[A] Returns a new iterator pointing to all elements in the iterator that satisfy condition p. | |
def filterNot(p: (A) => Boolean): Iterator[A] Returns an iterator pointing to the elements in the iterator that do not satisfy condition p. | |
##def find(p: (A) => Boolean): Option[A] Return the first element that satisfies p or None. Note: If an element satisfying the condition is found, the iterator will be placed after the element; if not found, it will be placed at the end. | |
def flatMap[B](f: (A) => GenTraversableOnce[B]): Iterator[ B] Applies function f to each element in the iterator's sequence and returns an iterator pointing to the resulting sequence. | |
def forall(p: (A) => Boolean): Boolean Returns a Boolean value indicating whether the elements pointed to by it satisfy p. | |
def foreach(f: (A) => Unit): Unit Execute the specified program on each element returned by the iterator f | |
def hasDefiniteSize: Boolean Returns true if the iterator has a limited number of elements (the default is equivalent to isEmpty) | |
def indexOf(elem: B): Int Returns the first element among the elements of the iterator with index equal to x. Note: the iterator will go over this element. | |
def indexWhere(p: (A) => Boolean): Int Return the element whose subscript satisfies the condition p among the elements of the iterator. Note: the iterator will go over this element. | |
def isEmpty: Boolean Check whether it is empty, return it if it is empty true, otherwise false (opposite of hasNext). | ##25|
def isTraversableAgain: Boolean | Tests whether this Iterator can be repeatedly traversed. | 26
def length: Int | Returns the number of iterator elements. | 27
def map[B](f: (A) => B): Iterator[B] | The result of passing each element in it to function f generates a new iterator. | 28
def max: A | Returns the largest element among the iterator elements . |
29 | def min: A Returns the smallest element among the iterator elements. |
30 | def mkString: String Convert all elements of the iterator into strings . |
31 | def mkString(sep: String): String will iterator all Elements are converted to strings and the delimiter is specified. |
32 | def nonEmpty: Boolean Checks whether the container contains elements (equivalent to hasNext). |
33 | def padTo(len: Int, elem: A): Iterator[A] First return all elements of the iterator, append and copy elem until the length reaches len. |
34 | def patch(from: Int, patchElems: Iterator[B], replaced: Int): Iterator[B ] Returns a new iterator in which the replaced elements starting from the from element are replaced by the element pointed to by the iterator. |
35 | def product: A Returns the product of the numeric elements indexed by the iterator . |
36 | ##def sameElements(that: Iterator[_]): Boolean Judgement Whether the iterator and the specified iterator parameters return the same elements in sequence |
def seq: Iterator[A] Returns the series view of the collection | |
def size: Int Return the number of elements of the iterator | |
##def slice(from: Int, until: Int): Iterator [A] Returns a new iterator pointing to the fragment starting from the from element and ending with the until element in the sequence pointed to by the iterator. | |
def sum: A Returns the sum of the numeric elements indexed by the iterator | |
def take(n: Int): Iterator[A] return A new iterator over the first n elements. | |
def toArray: Array[A] all the iterator points to The elements are placed into the array and returned. | |
def toBuffer: Buffer[B] All the iterator points to Elements are copied to the buffer Buffer. | |
def toIterable: Iterable[A] Returns an Iterable containing all elements of this traversable or iterator. This will not terminate for infinite iterators. | |
def toIterator: Iterator[A] Put all elements of the iterator into an Iterator container and return it. | |
def toList: List[A] put all elements of the iterator Arrange into a list and return | |
def toMap[T, U]: Map[T, U] Collect all the key-value pairs of the iterator into a Map and return it. | |
def toSeq: Seq[A] Put all the elements of the generator into a Seq container and return it. | |
49 | def toString(): String Convert the iterator to a string |
50 | def zip[B](that: Iterator[B]): Iterator[(A, B) Returns a new iterator pointing to a sequence of tuples formed by one-to-one correspondence between the iterator and the specified iterator that element |
For more methods, please refer to the API documentation
Scala collection