WEB DEVELOPER SITE

# Scala Iterator (iterator)

Scala Iterator is not a collection, it is a method for accessing collections.

The two basic operations of iterators are next and hasNext.

Calling it.next() returns the next element of the iterator and updates the state of the iterator.

Invoke it.hasNext() to detect if there are any elements in the collection.

The easiest way to get iterators to return all elements one by one is to use a while loop:

```object Test {
def main(args: Array[String]) {

while ( It.hasNext){
Println(it.next())
}
}
}```

Execute the above code, the output is:

```\$ scalac Test.< /span>scala
\$ scala Test
Baidu
welookups

## Find the largest and smallest elements

You can use the it.min and it.max methods to find the largest and smallest elements from the iterator, as shown in the following example:

```object Test {
def main(args: Array[String]) {
Val ita = Iterator(20,40,2,50,69, < Span class="lit">90)
Val itb = Iterator(20,40,2,50,69, < Span class="lit">90)

Println("The biggest element is:" + ita.max  )
Println("The smallest element is:" + itb.min  )

}
}```

Execute the above code, the output is:

```\$ scalac Test.< /span>scala
\$ scala Test
The largest element is:90
The smallest element is:2```

## Get the length of the iterator

You can use the it.size or it.length method to see the number of elements in the iterator. An example is as follows:

```object Test {
def main(args: Array[String]) {
Val ita = Iterator(20,40,2,50,69, < Span class="lit">90)
Val itb = Iterator(20,40,2,50,69, < Span class="lit">90)

Println("The value of ita.size: "  + ita.size )
Println("The value of itb.length: "  + itb.length )

}
}```

Execute the above code, the output is:

```\$ scalac Test.< /span>scala
\$ scala Test
Ita.size value: 6
Itb.length value: 6```

## Scala Iterator common method

The following table lists the commonly used methods for Scala Iterator:

serial number 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 of the iterator and updates the state 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

Add a string to StringBuilder b

6

def addString(b: StringBuilder, sep: String): StringBuilder

Add a string to StringBuilder b and specify a separator

7

def buffered: BufferedIterator[A]

iterators are converted to BufferedIterator

8

def contains(elem: Any): Boolean

Detect if the specified element is included in the iterator

9

def copyToArray(xs: Array[A], start: Int, len: Int): Unit

Pass the selected value from the iterator to the array

10

def count(p: (A) => Boolean): Int

Returns the total number of elements in the iterator element that satisfy condition p.

11

def drop(n: Int): Iterator[A]

Return the new set of n elements before discarding

12

def dropWhile(p: (A) => Boolean): Iterator[A]

Discard elements from left to right until condition p does not hold

13

def duplicate: (Iterator[A], Iterator[A])

Generate two iterators that return all the elements of the iterator.

14

def exists(p: (A) => Boolean): Boolean

Returns a Boolean value indicating whether there is an element in the iterator element that satisfies p.

15

def filter(p: (A) => Boolean): Iterator[A]

Returns a new iterator pointing to all elements in the iterator element that satisfy the condition p.

16

def filterNot(p: (A) => Boolean): Iterator[A]

Returns an iterator pointing to an element in the iterator element that does not satisfy condition p.

17

def find(p: (A) => Boolean): Option[A]

Returns the first element that satisfs p or None. Note: If an element that satisfies the condition is found, the iterator will be placed after the element; if it is not found, it will be placed at the end.

18

def flatMap[B](f: (A) => GenTraversableOnce[B]): Iterator[B]

Applies a function f to each element in the iterator's sequence and returns an iterator pointing to the result sequence.

19

def forall(p: (A) => Boolean): Boolean

Returns a Boolean value indicating whether the elements pointed to by it all satisfy p.

20

def foreach(f: (A) => Unit): Unit

Execute the specified program on each element returned by the iterator f

21

def hasDefiniteSize: Boolean

If the number of elements of the iterator is limited, it returns true (the default is equivalent to isEmpty)

22

def indexOf(elem: B): Int

Returns the first element of the element of the iterator whose index is equal to x. Note: The iterator will cross this element.

23

def indexWhere(p: (A) => Boolean): Int

Returns the elements of the iterator whose subscripts satisfy the condition p. Note: The iterator will cross this element.

24

def isEmpty: Boolean

Check if it is empty, return true for null, otherwise return false (as opposed to 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 produces a new iterator.

28

def max: A

Returns the largest element in the iterator iterator element.

29

def min: A

Returns the smallest element in the iterator iterator element.

30

def mkString: String

Convert all elements of the iterator to a string.

31

def mkString(sep: String): String

Convert all elements of the iterator to a string and specify a separator.

32

def nonEmpty: Boolean

Check if the container contains an element (equivalent to hasNext).

33

def padTo(len: Int, elem: A): Iterator[A]

First return all elements of the iterator, append 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 element starting at the from element is replaced by the element pointed to by the iterator.

35

def product: A

Returns the product of the exponential value elements of the iterator.

36

def sameElements(that: Iterator[_]): Boolean

Judge whether the iterator and the specified iterator parameters return the same elements in turn

37

def seq: Iterator[A]

Back to the series view of the collection

38

def size: Int

Number of elements returned to the iterator

39

def slice(from: Int, until: Int): Iterator[A]

Returns a new iterator pointing to the fragment in the sequence pointed to by the iterator starting at the from element and ending at the second element.

40

def sum: A

Returns the sum of the exponential value elements of the iterator

41

def take(n: Int): Iterator[A]

Returns a new iterator of the first n elements.

42

def toArray: Array[A]

Group all the elements pointed to by the iterator into an array and return.

43

def toBuffer: Buffer[B]

Copy all elements pointed to by the iterator to the 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]

Group all the elements of the iterator into an Iterator container and return.

46

def toList: List[A]

Group all the elements of the iterator into the list and return

47

def toMap[T, U]: Map[T, U]

Group all the key-value pairs of the iterator into a Map and return.

48

def toSeq: Seq[A]

Group all the elements of the generator into a Seq container and return.

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 two-tuple sequence that is one-to-one correspondence between the iterator and the specified iterator that element.