Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Scala Iterator

Scala Iterator

Scala Collections Scala Collections

Scala Iterator is not a collection; it is a way to access collections.

The two basic operations of iterator it are next and hasNext。

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

called it.hasNext() used to check whether there are more elements in the collection.

The simplest way to have iterator it return all elements one by one is to use a while loop:

Example

object

Test

{

   

def

main

(

args

:

Array

[

String

]

)

{

     

val

it

=

Iterator

(

"Baidu"

,

"Google"

,

"Runoob"

,

"Taobao"

)

     

     

while

(

it.

hasNext

)

{

         println

(

it.

next

(

)

)

     

}

   

}

}

The output of the code above is:



$ scalac Test.scala 

$ scala Test

Baidu

Google

Runoob

Taobao


Finding the Largest and Smallest Elements

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

Example

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

(

"The largest element is: "

max

)

      println

(

"The smallest element is: "

min

)

   

}

}

The output of the code above is:



$ scalac Test.scala 

$ scala Test

最大元素是:90

最小元素是:2


Getting the Length of an Iterator

You can use the it.size or it.length method to view the number of elements in an iterator. Example as follows:

Example

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

(

"The value of ita.size: "

size

)

      println

(

"The value of itb.length: "

length

)

   

}

}

The output of the code above is:



$ scalac Test.scala 

$ scala Test

ita.size 的值: 6

itb.length 的值: 6


Scala Iterator Common Methods

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

No. Method and description
1

def hasNext: Boolean

Returns true if there are more elements to return.

2

def next(): A

Returns the next element of the iterator and updates its state

3

def ++(that: => Iterator[A]): Iterator[A]

Combines two iterators

4

def ++[B >: A](that :=> GenTraversableOnce[B]): Iterator[B]

Combines two iterators

5

def addString(b: StringBuilder): StringBuilder

Adds a string to StringBuilder b

6

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

Adds a string to StringBuilder b with a specified separator

7

def buffered: BufferedIterator[A]

Converts iterators to BufferedIterator

8

def contains(elem: Any): Boolean

Tests whether the iterator contains a specified element

9

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

Copies selected values from the iterator to an array

10

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

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

11

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

Returns a new collection with the first n elements dropped

12

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

Drops elements from left to right until predicate p no longer holds

13

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

Creates two iterators that each return all elements of the iterator.

14

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

Returns a boolean indicating whether any element of the iterator satisfies p.

15

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

Returns a new iterator pointing to all elements of the iterator that satisfy p.

16

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

Returns an iterator pointing to elements of the iterator that do not satisfy p.

17

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

Returns the first element satisfying p, or None. Note: if a matching element is found, the iterator is positioned after that element; if not found, it is positioned at the end.

18

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

Applies function f to each element of the iterator's sequence and returns an iterator over the resulting sequence.

19

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

Returns a boolean indicating whether all elements pointed to by it satisfy p.

20

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

Executes the specified procedure f on each element returned by the iterator

21

def hasDefiniteSize: Boolean

Returns true if the iterator has a finite number of elements (by default equivalent to isEmpty)

22

def indexOf(elem: B): Int

Returns the first element of the iterator whose index equals x. Note: the iterator advances past this element.

23

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

Returns elements of the iterator whose index satisfies predicate p. Note: the iterator advances past this element.

24

def isEmpty: Boolean

Checks whether it is empty; returns true if empty, false otherwise (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]

Creates a new iterator from the results of passing each element of it through function f.

28

def max: A

Returns the largest element among the iterator's elements.

29

def min: A

Returns the smallest element among the iterator's elements.

30

def mkString: String

Converts all elements of the iterator to a string.

31

def mkString(sep: String): String

Converts all elements of the iterator to a string with a specified separator.

32

def nonEmpty: Boolean

Checks whether the collection contains an element (equivalent to hasNext).

33

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

Returns all elements of the iterator first, then appends copies of elem until the length reaches len.

34

def patch(from: Int, patchElems: Iterator[B], replaced: Int): Iterator[B]

Returns a new iterator in which replaced elements starting from index from are replaced by the elements pointed to by the iterator.

35

def product: A

Returns the product of the numeric elements pointed to by the iterator.

36

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

Determines whether the iterator and the specified iterator parameter return the same elements in the same order

37

def seq: Iterator[A]

Returns a series view of the collection

38

def size: Int

Returns the number of elements of the iterator

39

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

Returns a new iterator pointing to the slice of the iterator's sequence from index from (inclusive) to index until (exclusive).

40

def sum: A

Returns the sum of the numeric elements pointed to by the iterator

41

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

Returns a new iterator of the first n elements.

42

def toArray: Array[A]

Collects all elements pointed to by the iterator into an array and returns it.

43

def toBuffer: Buffer[B]

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

Collects all elements of the iterator into an Iterator container and returns it.

46

def toList: List[A]

Collects all elements of the iterator into a list and returns it

47

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

Collects all key-value pairs of the iterator into a Map and returns it.

48

def toSeq: Seq[A]

Collects all elements of the iterator into a Seq container and returns it.

49

def toString(): String

Converts 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 pairing elements of the iterator with elements of the specified iterator that

For more methods, refer to API Documentation

Scala Collections Scala Collections