The best Scala Iterator (iterators) Tutorial In 2024, In this tutorial you can learn Find the maximum and minimum elements,Get iterator length,Scala Iterator common method,
Scala Iterator (iterators) is not a collection, it is a method for access to the collection.
Two basic operations iterator it isnext and hasNext.
Callit.next () returns the next element in the iterator, and update the iterator state.
Callit.hasNext () is used to detect whether there are elements in the collection.
The iterator returns all the elements one by one the easiest way is to use a while loop:
object Test { def main(args: Array[String]) { val it = Iterator("Baidu", "Google", "w3big", "Taobao") while (it.hasNext){ println(it.next()) } } }
Implementation of the above code, the output is:
$ scalac Test.scala $ scala Test Baidu Google w3big Taobao
You can useit.min and it.maxmethod to find the maximum and minimum elements from the iterator, examples are 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 ) } }
Implementation of the above code, the output is:
$ scalac Test.scala $ scala Test 最大元素是:90 最小元素是:2
You can useit.size or it.lengthway to view the number of elements in the iterator. Examples are 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 ) } }
Implementation of the above code, the output is:
$ scalac Test.scala $ scala Test ita.size 的值: 6 itb.length 的值: 6
The following table lists the Scala Iterator common methods:
No. | Method and Description |
---|---|
1 | def hasNext: Boolean If there are elements that can be returned, return true. |
2 | def next (): A Returns the next element in the iterator, and updates the state of the iterator |
3 | def ++ (that: => Iterator [A]): Iterator [A] Merging two iterators |
4 | def ++ [B>: A] (that: => GenTraversableOnce [B]): Iterator [B] Merging two iterators |
5 | def addString (b: StringBuilder): StringBuilder Add a string to StringBuilder b |
6 | def addString (b: StringBuilder, sep : String): StringBuilder Add a string to StringBuilder b, and specify delimiters |
7 | def buffered: BufferedIterator [A] Iterators are converted into BufferedIterator |
8 | def contains (elem: Any): Boolean Detector iterator contains the specified element |
9 | def copyToArray (xs: Array [A ], start: Int, len: Int): Unit Iterator selected value is passed to an array |
10 | def count (p: (A) => Boolean): Int Returns the total number of elements in the iterator element satisfies the condition p. |
11 | def drop (n: Int): Iterator [A] The new collection before returning discards n elements |
12 | def dropWhile (p: (A) => Boolean): Iterator [A] Discard element left to right, until the condition is not satisfied p |
13 | def duplicate: (Iterator [A] , Iterator [A]) Two were able to generate returns an iterator iterator of all the elements. |
14 | def exists (p: (A) => Boolean): Boolean It returns a Boolean value indicating whether there are elements of the iterator satisfy p elements. |
15 | def filter (p: (A) => Boolean): Iterator [A] Return a new iterator iterator element in all elements to meet the conditions of p. |
16 | def filterNot (p: (A) => Boolean): Iterator [A] Returns an iterator iterator element does not satisfy the condition p elements. |
17 | def find (p: (A) => Boolean): Option [A] Returns the first element satisfying p or None. NOTE: If you meet the conditions of the elements found, the iterator will be placed after the element; if not found, it will be placed in the end. |
18 | def flatMap [B] (f: (A) => GenTraversableOnce [B]): Iterator [B] For iterators applied to each element in the sequence function f, and returns a pointer to the result sequence iterators. |
19 | def forall (p: (A) => Boolean): Boolean It returns a Boolean value that indicates whether it refers to the elements satisfy p. |
20 | def foreach (f: (A) => Unit): Unit Perform specified on each element returned by the iterator program f |
twenty one | def hasDefiniteSize: Boolean If the number of finite element iterator returns true (the default is equivalent to isEmpty) |
twenty two | def indexOf (elem: B): Int Returns an iterator of the elements in the index is equal to the first element of x. Note: The iterator will cross this element. |
twenty three | def indexWhere (p: (A) => Boolean): Int Returns an iterator of the elements in the index to meet the conditions p elements. Note: The iterator will cross this element. |
twenty four | def isEmpty: Boolean Check if it is empty, empty returns true, otherwise it returns false (opposite of hasNext). |
25 | def isTraversableAgain: Boolean Tests whether this Iterator can be repeatedly traversed. |
26 | def length: Int Returns the number of elements in the iterator. |
27 | def map [B] (f: (A) => B): Iterator [B] The results after it passed into the function f of each element to generate a new iterator. |
28 | def max: A Returns an iterator iterator element largest element. |
29 | def min: A Returns an iterator iterator element in the smallest elements. |
30 | def mkString: String Convert iterators all the elements to a string. |
31 | def mkString (sep: String): String All the elements to convert an iterator to a string, and specify delimiters. |
32 | def nonEmpty: Boolean Check whether the container contains elements (the equivalent of hasNext). |
33 | def padTo (len: Int, elem : A): Iterator [A] First of all the elements returns an iterator, an additional copy of elem until the length reaches len. |
34 | def patch (from: Int, patchElems : Iterator [B], replaced: Int): Iterator [B] Return a new iterator, which replaced elements from the first element from the beginning to be replaced by elements within the meaning of iterators. |
35 | def product: A Return product within the meaning of the numerical element iterators. |
36 | def sameElements (that: Iterator [_ ]): Boolean Analyzing iterators and iterator parameters are specified in turn returns the same element |
37 | def seq: Iterator [A] Back view of the collection series |
38 | def size: Int Returns the number of elements in an iterator |
39 | def slice (from: Int, until : Int): Iterator [A] Return a new iterator, an iterator is pointing to the sequence fragment from the start to the first element from an end to the first until elements. |
40 | def sum: A Returns an iterator referring to numerical elements and |
41 | def take (n: Int): Iterator [A] Returns a new iterator first n elements. |
42 | def toArray: Array [A] All elements iterator is pointing into an array and returns. |
43 | def toBuffer: Buffer [B] All elements iterator is pointing to the copy buffer Buffer. |
44 | def toIterable: Iterable [A] Returns an Iterable containing all elements of this traversable or iterator. This will not terminate for infinite iterators. |
45 | def toIterator: Iterator [A] All elements Iterator iterator into a container and returned. |
46 | def toList: List [A] All the elements of the iterator into a list and returns |
47 | def toMap [T, U]: Map [T, U] All of the key iterator into a Map and returns. |
48 | def toSeq: Seq [A] All elements throughout the container into a Seq and returns. |
49 | def toString (): String Iterator converted to a string |
50 | def zip [B] (that: Iterator [B]): Iterator [(A, B) Return a new iterator pointing tuple sequence respectively correspond to the specified iterators and iterators that element from the |
More ways to refer to the API documentation