scala.Iterator

trait Iterator

trait Iterator[+ A]

 extends ScalaObject


Iterators are data structures that allow to iterate over a sequence of elements. They have a hasNext method for checking if there is a next element available, and a next method which returns the next element and discards it from the iterator.
author
- Martin Odersky, Matthias Zenger
version
- 1.2, 15/03/2004


Companion: Iterator

Source: Iterator.scala(220)

Direct Known Subclasses

Source, BufferedIterator, SimpleTokenizer, XMLEventReader, AnyIterator, MatchIterator, CountedIterator, MutableIterator, CharInputStreamIterator,

Nested Classes

PredicatedIterator, TakeWhileIterator,

 Fields

buffered def buffered
Returns a buffered iterator from this iterator
BufferedIterator[A]
collect def collect
Collect elements into a seq [details]
return
- a seq which enumerates all elements of this iterator.
Seq[A]
counted def counted
Returns a counted iterator from this iterator
CountedIterator[A]
hasNext abstract def hasNext
Does this iterator provide another element?
Boolean
mkString def mkString
Returns a string representation of this iterable object [details]
The string representations of elements (w.r.t. the method toString()) are separated by a comma.
return
- a string representation of this iterable object.
String
toList def toList
Transform this iterator into a list of all elements [details]
return
- a list which enumerates all elements of this iterator.
List[A]

 Methods

!= final def !=(arg0 : Object) Boolean AnyRef
!= final def !=(arg0 : Any)
o != arg0 is the same as !(o == (arg0)) [details]

param
arg0 - the object to compare against this object for dis-equality.
return
- false if the receiver object is equivalent to the argument; true otherwise.

Boolean Any
++ def ++[B >: A](that : => Iterator[B])
Returns a new iterator that first yields the elements of this iterator followed by the elements provided by iterator that
Iterator[B]
/: def /:[B](z : B)(op : (B, A) => B)
Similar to foldLeft but can be used as an operator with the order of iterator and zero arguments reversed [details]
That is, z /: xs is the same as xs foldLeft z.
param
op - the applied operator.
z - the left argument of the first application of op (evaluation occurs from left to right).
return
- the result value
see
- foldLeft.
B
:\ def :\[B](z : B)(op : (A, B) => B)
An alias for foldRight [details]
That is, xs :\ z is the same as xs foldRight z.
param
op - the applied operator.
z - the right argument of the first application of op (evaluation occurs from right to left).
return
- the result value.
see
- foldRight.
B
== final def ==(arg0 : Any)
o == arg0 is the same as o [details]
equals(arg0).

param
arg0 - the object to compare against this object for equality.
return
- true if the receiver object is equivalent to the argument; false otherwise.

Boolean Any
== final def ==(arg0 : Object)
o == arg0 is the same as if (o eq null) arg0 eq null else o [details]
equals(arg0).

param
arg0 - the object to compare against this object for equality.
return
- true if the receiver object is equivalent to the argument; false otherwise.

Boolean AnyRef
addString def addString(buf : StringBuilder, start : String, sep : String, end : String)
Write all elements of this string into given string builder [details]
param
end - the ending string
start - the starting string
sep - the separator string
buf - ...
return
- ...
StringBuilder
append def append[B >: A](that : Iterator[B])
Returns a new iterator that first yields the elements of this iterator followed by the elements provided by iterator that [details]
deprecated
- use ++
Iterator[B]
asInstanceOf final def asInstanceOf[T0]
This method is used to cast the receiver object to be of type T0 [details]

Note that the success of a cast at runtime is modulo Scala's erasure semantics. Therefore the expression 1.asInstanceOf[String] will throw a ClassCastException at runtime, while the expression List(1).asInstanceOf[List[String]] will not. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the requested typed.

return
- the receiver object.

throws
ClassCastException - if the receiver object is not an instance of erasure of type T0.
T0 Any
clone protected def clone
This method creates and returns a copy of the receiver object [details]

The default implementation of the clone method is platform dependent.

return
- a copy of the receiver object.

Object AnyRef
contains def contains(elem : Any)
Tests if the given value elem is a member of this iterator [details]
param
elem - element whose membership has to be tested.
return
- true iff there is an element of this iterator which is equal (w.r.t. ==) to elem.
Boolean
copyToArray def copyToArray[B >: A](xs : Array[B], start : Int)
Fills the given array xs with the elements of this sequence starting at position start [details]
param
start - the starting index.
xs - the array to fill.
pre
- the array must be large enough to hold all elements.
Unit
copyToBuffer def copyToBuffer[B >: A](dest : Buffer[B])
Copy all elements to a buffer [details]
param
The - buffer to which elements are copied
return
- The buffer to which elements are copied
Unit
drop def drop(n : Int)
Removes the first n elements from this iterator [details]
param
n - the number of elements to drop
return
- the new iterator
Iterator[A]
dropWhile def dropWhile(p : (A) => Boolean)
Skips longest sequence of elements of this iterator which satisfy given predicate p, and returns an iterator of the remaining elements [details]
param
p - the predicate used to skip elements.
return
- an iterator consisting of the remaining elements
Iterator[A]
duplicate def duplicate
Creates two new iterators that both iterate over the same elements than this iterator (in the same order) [details]
return
- a pair of iterators
(Iterator[A], Iterator[A])
eq final def eq(arg0 : Object)
This method is used to test whether the argument (arg0) is a reference to the receiver object (this) [details]

The eq method implements an equivalence relation on non-null instances of AnyRef:

  • It is reflexive: for any non-null instance x of type AnyRef, x.eq(x) returns true.
  • It is symmetric: for any non-null instances x and y of type AnyRef, x.eq(y) returns true if and only if y.eq(x) returns true.
  • It is transitive: for any non-null instances x, y, and z of type AnyRef if x.eq(y) returns true and y.eq(z) returns true, then x.eq(z) returns true.
Additionally, the eq method has three other properties.
  • It is consistent: for any non-null instances x and y of type AnyRef, multiple invocations of x.eq(y) consistently returns true or consistently returns false.
  • For any non-null instance x of type AnyRef, x.eq(null) and null.eq(x) returns false.
  • null.eq(null) returns true.

When overriding the equals or hashCode methods, it is important to ensure that their behavior is consistent with reference equality. Therefore, if two objects are references to each other (o1 eq o2), they should be equal to each other (o1 == o2) and they should hash to the same value (o1.hashCode == o2.hashCode).

param
arg0 - the object to compare against this object for reference equality.
return
- true if the argument is a reference to the receiver object; false otherwise.

Boolean AnyRef
equals def equals(arg0 : Any)
This method is used to compare the receiver object (this) with the argument object (arg0) for equivalence [details]

The default implementations of this method is an equivalence relation:

  • It is reflexive: for any instance x of type Any, x.equals(x) should return true.
  • It is symmetric: for any instances x and y of type Any, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any instances x, y, and z of type AnyRef if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.

If you override this method, you should verify that your implementation remains an equivalence relation. Additionally, when overriding this method it is often necessary to override hashCode to ensure that objects that are "equal" (o1.equals(o2) returns true) hash to the same Int (o1.hashCode.equals(o2.hashCode)).

param
arg0 - the object to compare against this object for equality.
return
- true if the receiver object is equivalent to the argument; false otherwise.

Boolean AnyRef
exists def exists(p : (A) => Boolean)
Apply a predicate p to all elements of this iterable object and return true, iff there is at least one element for which p yields true [details]
param
p - the predicate
return
- true iff the predicate yields true for at least one element.
Boolean
filter def filter(p : (A) => Boolean)
Returns an iterator over all the elements of this iterator that satisfy the predicate p [details]
The order of the elements is preserved.
param
p - the predicate used to filter the iterator.
return
- the elements of this iterator satisfying p.
Iterator[A]
finalize protected def finalize
This method is called by the garbage collector on the receiver object when garbage collection determines that there are no more references to the object [details]

The details of when and if the finalize method are invoked, as well as the interaction between finalize and non-local returns and exceptions, are all platform dependent.

Unit AnyRef
find def find(p : (A) => Boolean)
Find and return the first element of the iterable object satisfying a predicate, if any [details]
param
p - the predicate
return
- the first element in the iterable object satisfying p, or None if none exists.
Option[A]
findIndexOf def findIndexOf(p : (A) => Boolean)
Returns index of the first element satisying a predicate, or -1 [details]
note
- may not terminate for infinite-sized collections.
param
p - the predicate
return
- the index of the first element satisfying p, or -1 if such an element does not exist
Int
flatMap def flatMap[B](f : (A) => Iterator[B])
Applies the given function f to each element of this iterator, then concatenates the results [details]
param
f - the function to apply on each element.
return
- an iterator over f(a0), ... , f(an) if this iterator yields the elements a0, ..., an.
Iterator[B]
foldLeft def foldLeft[B](z : B)(op : (B, A) => B)
Combines the elements of this iterator together using the binary operator op, from left to right, and starting with the value z [details]
return
- op(... (op(op(z,a0),a1) ...), an) if the iterator yields elements a0, a1, ..., an.
B
foldRight def foldRight[B](z : B)(op : (A, B) => B)
Combines the elements of this iterator together using the binary operator op, from right to left, and starting with the value z [details]
return
- a0 op (... op (an op z)...) if the iterator yields elements a0, a1, ..., an.
B
forall def forall(p : (A) => Boolean)
Apply a predicate p to all elements of this iterable object and return true iff the predicate yields true for all elements [details]
param
p - the predicate
return
- true iff the predicate yields true for all elements.
Boolean
foreach def foreach(f : (A) => Unit)
Apply a function f to all elements of this iterable object [details]
param
f - a function that is applied to every element.
Unit
getClass final def getClass
Returns a representation that corresponds to the dynamic class of the receiver object [details]

The nature of the representation is platform dependent.

return
- a representation that corresponds to the dynamic class of the receiver object.

Class[Any] AnyRef
hashCode def hashCode
Returns a hash code value for the object [details]

The default hashing algorithm is platform dependent. Note that it is allowed for two objects to have identical hash codes (o1.hashCode.equals(o2.hashCode)) yet not be equal (o1.equals(o2) returns false). A degenerate implementation could always return 0. However, it is required that if two objects are equal (o1.equals(o2) returns true) that they have identical hash codes (o1.hashCode.equals(o2.hashCode)). Therefore, when overriding this method, be sure to verify that the behavior is consistent with the equals method.

return
- the hash code value for the object.

Int AnyRef
indexOf def indexOf[B >: A](elem : B)
Returns the index of the first occurence of the specified object in this iterable object [details]
note
- may not terminate for infinite-sized collections.
param
elem - element to search for.
return
- the index in this sequence of the first occurence of the specified element, or -1 if the sequence does not contain this element.
Int
isInstanceOf final def isInstanceOf[T0]
This method is used to test whether the dynamic type of the receiver object is T0 [details]

Note that the test result of the test is modulo Scala's erasure semantics. Therefore the expression 1.isInstanceOf[String] will return false, while the expression List(1).isInstanceOf[List[String]] will return true. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the requested typed.

return
- true if the receiver object is an instance of erasure of type T0; false otherwise.
Boolean Any
map def map[B](f : (A) => B)
Returns a new iterator that maps all elements of this iterator to new elements using function f
Iterator[B]
mkString def mkString(sep : String)
Returns a string representation of this iterable object [details]
The string representations of elements (w.r.t. the method toString()) are separated by the string sep.
param
sep - separator string.
return
- a string representation of this iterable object.
String
mkString def mkString(start : String, sep : String, end : String)
Returns a string representation of the elements in this iterator [details]
The resulting string begins with the string start and is finished by the string end. Inside, the string representations of elements (w.r.t. the method toString()) are separated by the string sep.

Ex:
List(1, 2, 3).mkString("(", "; ", ")") = "(1; 2; 3)"

param
sep - separator string.
end - ending string.
start - starting string.
return
- a string representation of this iterable object.
String
ne final def ne(arg0 : Object)
o [details]
ne(arg0) is the same as !(o.eq(arg0)).

param
arg0 - the object to compare against this object for reference dis-equality.
return
- false if the argument is not a reference to the receiver object; true otherwise.

Boolean AnyRef
next abstract def next
Returns the next element
A
notify final def notify
Wakes up a single thread that is waiting on the receiver object's monitor
Unit AnyRef
notifyAll final def notifyAll
Wakes up all threads that are waiting on the receiver object's monitor
Unit AnyRef
readInto def readInto[B >: A](xs : Array[B], start : Int) Unit
readInto def readInto[B >: A](xs : Array[B]) Unit
readInto def readInto[B >: A](xs : Array[B], start : Int, sz : Int)
Fills the given array xs with the elements of this sequence starting at position start [details]
Like copyToArray, but designed to accomodate IO stream operations.
param
start - the starting index.
sz - the maximum number of elements to be read.
xs - the array to fill.
pre
- the array must be large enough to hold sz elements.
Unit
reduceLeft def reduceLeft[B >: A](op : (B, A) => B)
Combines the elements of this iterator together using the binary operator op, from left to right [details]
param
op - The operator to apply
return
- op(... op(a0,a1), ..., an) if the iterator yields elements a0, a1, ..., an.
throws
Predef.UnsupportedOperationException - if the iterator is empty.
B
reduceRight def reduceRight[B >: A](op : (A, B) => B)
Combines the elements of this iterator together using the binary operator op, from right to left [details]
param
op - The operator to apply
return
- a0 op (... op (an-1 op an)...) if the iterator yields elements a0, a1, ..., an.
throws
Predef.UnsupportedOperationException - if the iterator is empty.
B
slice def slice(from : Int, until : Int)
A sub-iterator of until - from elements starting at index from [details]
param
until - The index of the element following the slice
from - The index of the first element of the slice
Iterator[A]
synchronized final def synchronized[T0](arg0 : T0) T0 AnyRef
take def take(n : Int)
Returns a new iterator that iterates only over the first n elements [details]
param
n - the number of elements to take
return
- the new iterator
Iterator[A]
takeWhile def takeWhile(p : (A) => Boolean)
Returns an iterator over the longest prefix of this iterator such that all elements of the result satisfy the predicate p [details]
The order of the elements is preserved.
param
p - the predicate used to filter the iterator.
return
- the longest prefix of this iterator satisfying p.
Iterator[A]
toString override def toString
Returns a string representation of the object [details]

The default representation is platform dependent.

return
- a string representation of the object.

String
wait final def wait(arg0 : Long, arg1 : Int) Unit AnyRef
wait final def wait(arg0 : Long) Unit AnyRef
wait final def wait Unit AnyRef
zip def zip[B](that : Iterator[B])
Return an iterator formed from this iterator and the specified iterator that by associating each element of the former with the element at the same position in the latter [details]
If one of the two iterators is longer than the other, its remaining elements are ignored.
return
- an iterator yielding {a0,b0}, {a1,b1}, ... where ai are the elements from this iterator and bi are the elements from iterator that.
Iterator[(A, B)]
zipWithIndex def zipWithIndex
Return an iterator that pairs each element of this iterator with its index, counting from 0 [details]
return
- an iterator yielding {a0,0}, {a1,1}... where ai are the elements from this iterator.
Iterator[(A, Int)]