
Note: it will be at its end after this operation! Test whether the collection contains elements (alias of hasNext).

Test whether the iterator is empty (opposite of hasNext). The iterator obtained from applying the partial function f to every element in it for which it is defined and collecting the results.Ĭollects the elements returned by it in an array.Ĭollects the elements returned by it in a list.Ĭollects the elements returned by it in an iterable.Ĭollects the elements returned by it in a sequence.Ĭollects the elements returned by it in an indexed sequence.Ĭollects the elements returned by it in a stream.Ĭollects the elements returned by it in a set.Ĭollects the key/value pairs returned by it in a map.Ĭopies all elements returned by it to buffer buf.Ĭopies at most n elements returned by it to array arr starting at index s. The iterator obtained from applying the iterator-valued function f to every element in it and appending the results. The iterator obtained from applying the function f to every element returned from it. The iterator that first returns all elements of it and then follows that by copies of x until length len elements are returned overall. Returns true if it can return another element.Ī buffered iterator returning all elements of it.Īn iterator that yields the elements returned by it in fixed-sized sequence “chunks”.Īn iterator that yields the elements returned by it in sequences representing a sliding fixed-sized window.Ī pair of iterators that each independently return all elements of it.Īn iterator returning all elements returned by iterator it, followed by all elements returned by iterator jt. Returns next element on iterator and advances past it. It takes a TraversableOnce parameter, so you can append elements coming from either an iterator or a traversable collection.Īll operations on iterators are summarized below. An example is the appending method ++ in class Traversable. A common use case of TraversableOnce is as an argument type for methods that can take either an iterator or a traversable as argument. If the TraversableOnce object is in fact an Iterator, it will be at its end after the traversal, but if it is a Traversable, it will still exist as before. As the name implies, TraversableOnce objects can be traversed using foreach but the state of that object after the traversal is not specified. The Scala collection libraries make this explicit with an abstraction TraversableOnce, which is a common superclass of Traversable and Iterator. In summary, iterators behave like collections if one never accesses an iterator again after invoking a method on it.

The elements twice, but the effect is achieved through internal buffering.Īs usual, the underlying iterator it cannot be used directly and must be discarded.

This creates the illusion of iterating over The two iterators work independently: advancing one does not affect the other, so that each can beĭestructively modified by invoking arbitrary methods. Scala> val shorts = words.filter(_.length val count = ns.map(_.length).sum Scala> val (words, ns) = Iterator("a", "number", "of", "words").duplicate
