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", "Ceodata", "Yahoo")
while (it.hasNext){
println(it.next())
}
}
}
上記のコードを実行した出力結果は次となります。
$ scalac Test.scala
$ scala Test
Baidu
Google
Ceodata
Yahoo
目次
最大要素と最小要素を見つける
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 イテレータの要素にあるindexがxに等しい最初の要素を返します。ヒント:イテレータはこの要素を渡します。 |
23 | def indexWhere(p: (A) => Boolean): Int イテレータの要素の中で、添え字が条件pを満たす要素を返します。ヒント:イテレータはこの要素を渡します。 |
24 | def isEmpty: Boolean itは空白であるかどうかを確認し、空白の場合はtrueを返し、空白でない場合はfalseを返します。(hasNextは正反対です) |
25 | def isTraversableAgain: Boolean このイテレータを繰り返しスクロールできるかどうかをテストします。 |
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] このスクロールまたはイテレータにあるすべての要素を含むIterableを返します。無限に続くイテレータの場合、終了しません。 |
45 | def toIterator: Iterator[A] イテレータにあるすべての要素をIteratorコンテナに入れて返します。 |
46 | def toList: List[A] イテレータのすべての要素をリストに入れて返します。 |
47 | def toMap[T, U]: Map[T, U] イテレータにあるすべての(キー、値)ペアをMapに入れて返します。 |
48 | def toSeq: Seq[A] イテレータにあるすべての要素をSeqコンテナに入れて返します。 |
49 | def toString(): String イテレータを文字列に変換します。 |
50 | def zip[B](that: Iterator[B]): Iterator[(A, B) 新しいイテレータを返し、それぞれイテレータと指定されたイテレータのthat要素に指します。(対応するタプルのシーケンスを指すます) |
コメントを残す