Scala Iterator(イテレータ)

Scala 集合Scala コレクション

Scala Iterator(イテレータ)はコレクションではなく、コレクションにアクセスするための方法です。

イテレータ it の2つの基本操作は次のとおりです:nextとhasNext。

呼び出しit.next()はイテレータの次の要素を返し、イテレータの状態を更新します。

呼び出しit.hasNext()はコレクションにまだ要素があるかどうかを検出するために使用します。

イテレータ it にすべての要素を1つずつ返させる最も簡単な方法は、while ループを使用することです:

例

object Test {
   def main(args: Array[String]) {
      val it = Iterator("Baidu", "Google", "サンプル", "Taobao")
     
      while (it.hasNext){
         println(it.next())
      }
   }
}

上記のコードを実行すると、出力結果は次のようになります:

$ scalac Test.scala 
$ scala Test
Baidu
Google
サンプル
Taobao

最大と最小の要素を検索する

次のものを使用できます:it.minとit.maxメソッドを使用してイテレータから最大と最小の要素を検索します。例は次のとおりです:

例

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 )

   }
}

上記のコードを実行すると、出力結果は次のようになります:

$ scalac Test.scala 
$ scala Test
最大元素是:90
最小元素是:2

イテレータの長さを取得する

次のものを使用できます:it.size 或 it.lengthメソッドを使用して、イテレータ内の要素の数を確認します。例は次のとおりです:

例

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 )

   }
}

上記のコードを実行すると、出力結果は次のようになります:

$ scalac Test.scala 
$ scala Test
ita.size 的值: 6
itb.length 的值: 6

Scala Iterator のよく使うメソッド

次の表に、Scala Iterator でよく使われるメソッドを示します:

番号 メソッドと説明
1

def hasNext: Boolean

まだ返すことができる要素があれば、true を返します。

2

def next(): A

イテレータの次の要素を返し、イテレータの状態を更新します

3

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

2つのイテレータを結合します

4

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

2つのイテレータを結合します

5

def addString(b: StringBuilder): StringBuilder

StringBuilder b に文字列を追加します

6

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

StringBuilder b に文字列を追加し、区切り文字を指定します

7

def buffered: BufferedIterator[A]

イテレータをすべて BufferedIterator に変換します

8

def contains(elem: Any): Boolean

イテレータに指定された要素が含まれているかどうかを検出します

9

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

イテレータ内の選択された値を配列に渡します

10

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

イテレータの要素のうち、条件 p を満たす要素の総数を返します。

11

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

先頭の n 個の要素を破棄した新しいコレクションを返します

12

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

左から右に、条件 p が成立しなくなるまで要素を破棄します

13

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

それぞれがイテレータのすべての要素を返すことができる2つのイテレータを生成します。

14

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

イテレータの要素に p を満たす要素が存在するかどうかを示すブール値を返します。

15

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

イテレータの要素のうち、条件 p を満たすすべての要素を指す新しいイテレータを返します。

16

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

イテレータの要素のうち、条件 p を満たさない要素を指すイテレータを返します。

17

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

p を満たす最初の要素または None を返します。注意:条件を満たす要素が見つかった場合、イテレータはその要素の後に置かれます。見つからなかった場合は、終点に置かれます。

18

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

イテレータのシーケンス内の各要素に関数 f を適用し、結果のシーケンスを指すイテレータを返します。

19

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

it が指す要素がすべて p を満たすかどうかを示すブール値を返します。

20

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

イテレータが返す各要素に対して指定された手続き f を実行します

21

def hasDefiniteSize: Boolean

イテレータの要素数が有限であれば true を返します(デフォルトでは isEmpty と同じ)

22

def indexOf(elem: B): Int

イテレータの要素のうち、インデックスが x に等しい最初の要素を返します。注意:イテレータはこの要素を越えます。

23

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

イテレータの要素のうち、インデックスが条件 p を満たす要素を返します。注意:イテレータはこの要素を越えます。

24

def isEmpty: Boolean

it が空かどうかを確認し、空であれば true を返し、そうでなければ false を返します(hasNext の逆)。

25

def isTraversableAgain: Boolean

Tests whether this Iterator can be repeatedly traversed.

26

def length: Int

イテレータの要素の数を返します。

27

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

it の各要素を関数 f に渡した結果から新しいイテレータを生成します。

28

def max: A

イテレータの要素の中で最大の要素を返します。

29

def min: A

イテレータの要素の中で最小の要素を返します。

30

def mkString: String

イテレータのすべての要素を文字列に変換します。

31

def mkString(sep: String): String

イテレータのすべての要素を文字列に変換し、区切り文字を指定します。

32

def nonEmpty: Boolean

コンテナに要素が含まれているかどうかを確認します(hasNext に相当)。

33

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

まずイテレータのすべての要素を返し、長さが len に達するまで elem のコピーを追加します。

34

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

from 番目の要素から始まる replaced 個の要素が、イテレータが指す要素で置き換えられた新しいイテレータを返します。

35

def product: A

イテレータが指す数値型の要素の積を返します。

36

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

イテレータと指定されたイテレータ引数が同じ要素を順に返すかどうかを判定します

37

def seq: Iterator[A]

コレクションのシーケンシャルビューを返します。

38

def size: Int

イテレータの要素の数を返します

39

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

イテレータが指すシーケンスのうち、from 番目の要素から始まり until 番目の要素で終わる部分を指す新しいイテレータを返します。

40

def sum: A

イテレータが指す数値型の要素の合計を返します

41

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

先頭の n 個の要素を持つ新しいイテレータを返します。

42

def toArray: Array[A]

イテレータが指すすべての要素を配列に格納して返します。

43

def toBuffer: Buffer[B]

イテレータが指すすべての要素をバッファ 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]

イテレータのすべての要素を1つの Iterator コンテナに格納して返します。

46

def toList: List[A]

イテレータのすべての要素をリストに格納して返します

47

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

イテレータのすべてのキーと値のペアを1つの Map に格納して返します。

48

def toSeq: Seq[A]

イテレータのすべての要素を1つの Seq コンテナに格納して返します。

49

def toString(): String

イテレータを文字列に変換します

50

def zip[B](that: Iterator[B]): Iterator[(A, B)

イテレータと指定されたイテレータ that の要素を1つずつ対応させてできるペアのシーケンスを指す新しいイテレータを返します

その他のメソッドについては、以下を参照できます:API ドキュメント

Scala 集合Scala コレクション

その他の拡張