kotlin
Extensions on java.util.Iterator

extension functions on class java.util.Iterator
from package kotlin

Function Summary
fun <T>
Iterator<T>
all(predicate: (T) -> Boolean): Boolean
Returns true if all elements match the given predicate
fun <T>
Iterator<T>
any(predicate: (T) -> Boolean): Boolean
Returns true if any elements match the given predicate
fun <T>
Iterator<T>
appendString(buffer: Appendable, [separator: String, prefix: String, postfix: String, limit: Int, truncated: String]): Unit
Appends the string from all the elements separated using the separator and using the given prefix and postfix if supplied
fun <T>
Iterator<T>
count(predicate: (T) -> Boolean): Int
Returns the number of elements which match the given predicate
fun <T, L>
Iterator<T>
dropWhileTo(result: L, predicate: (T) -> Boolean): L
Returns a list containing the everything but the first elements that satisfy the given predicate
fun <T>
Iterator<T>
filter(predicate: (T) -> Boolean): Iterator<T>
Returns an iterator over elements which match the given predicate
fun <T, R>
Iterator<T>
filterIsInstance(klass: Class<R>): Iterator<R>
Returns an iterator over elements that are instances of a given type R which is a subclass of T
fun <T>
Iterator<T>
filterNot(predicate: (T) -> Boolean): Iterator<T>
Returns an iterator over elements which do not match the given predicate
fun <T>
Iterator<T?>
filterNotNull(): Iterator<T>
Returns an iterator over non-null elements
fun <T, C>
Iterator<T?>
filterNotNullTo(result: C): C
Filters all non-null elements into the given list
fun <T, C>
Iterator<T>
filterNotTo(result: C, predicate: (T) -> Boolean): C
Returns a list containing all elements which do not match the given predicate
fun <T, C>
Iterator<T>
filterTo(result: C, predicate: (T) -> Boolean): C
Filters all elements which match the given predicate into the given list
fun <T>
Iterator<T>
find(predicate: (T) -> Boolean): T?
Returns the first element which matches the given predicate or null if none matched
fun <T, R>
Iterator<T>
flatMap(transform: (T) -> Iterator<R>): Iterator<R>
Returns an iterator over the concatenated results of transforming each element to one or more values
fun <T, R>
Iterator<T>
flatMapTo(result: Collection<R>, transform: (T) -> Collection<R>): Collection<R>
Returns the result of transforming each element to one or more values which are concatenated together into a single list
fun <T>
Iterator<T>
fold(initial: T, operation: (T, T) -> T): T
Folds all elements from from left to right with the initial value to perform the operation on sequential pairs of elements
fun <T>
Iterator<T>
foldRight(initial: T, operation: (T, T) -> T): T
Folds all elements from right to left with the initial value to perform the operation on sequential pairs of elements
fun <T>
Iterator<T>
forEach(operation: (T) -> Unit): Unit
Performs the given operation on each element
fun <T, K>
Iterator<T>
groupBy(toKey: (T) -> K): Map<K, List<T>>
Groups the elements in the collection into a new Map using the supplied toKey function to calculate the key to group the elements by
fun <T, K>
Iterator<T>
groupByTo(result: Map<K, List<T>>, toKey: (T) -> K): Map<K, List<T>>
Groups the elements in the collection into the given Map using the supplied toKey function to calculate the key to group the elements by
fun <T>
Iterator<T>
iterator(): Iterator<T>
Helper to make java.util.Iterator usable in for
fun <T>
Iterator<T>
makeString( [separator: String, prefix: String, postfix: String, limit: Int, truncated: String]): String
Creates a string from all the elements separated using the separator and using the given prefix and postfix if supplied.
fun <T, R>
Iterator<T>
map(transform: (T) -> R): Iterator<R>
Returns an iterator obtained by applying transform, a function transforming an object of type T into an object of type R
fun <T>
Iterator<T>
plus(element: T): Iterator<T>
Creates an Iterator which iterates over this iterator then the given element at the end
fun <T>
Iterator<T>
plus(collection: Iterable<T>): Iterator<T>
Creates an Iterator which iterates over this iterator then the following collection
fun <T>
Iterator<T>
plus(iterator: Iterator<T>): Iterator<T>
Creates an Iterator which iterates over this iterator then the following iterator
fun <T>
Iterator<T>
reduce(operation: (T, T) -> T): T
Applies binary operation to all elements of iterable, going from left to right.
Similar to fold function, but uses the first element as initial value
fun <T>
Iterator<T>
reduceRight(operation: (T, T) -> T): T
Applies binary operation to all elements of iterable, going from right to left.
Similar to foldRight function, but uses the last element as initial value
fun <T>
Iterator<T?>
requireNoNulls(): Iterator<T>
Returns an iterator containing all the non-null elements, lazily throwing an IllegalArgumentException
if there are any null elements
fun <T>
Iterator<T>
reverse(): List<T>
Reverses the order the elements into a list
fun <T>
Iterator<T>
take(n: Int): Iterator<T>
Returns an iterator restricted to the first n elements
fun <T>
Iterator<T>
takeWhile(predicate: (T) -> Boolean): Iterator<T>
Returns an iterator restricted to the first elements that match the given predicate
fun <T, C>
Iterator<T>
takeWhileTo(result: C, predicate: (T) -> Boolean): C
Returns a list containing the first elements that satisfy the given predicate
fun <T>
Iterator<T>
toCollection(): Collection<T>
Copies all elements into a [[List]
fun <T, C>
Iterator<T>
toCollection(result: C): C
Copies all elements into the given collection
fun <T>
Iterator<T>
toLinkedList(): LinkedList<T>
Copies all elements into a LinkedList
fun <T>
Iterator<T>
toList(): List<T>
Copies all elements into a List
fun <T>
Iterator<T>
toSet(): Set<T>
Copies all elements into a Set
fun <T>
Iterator<T>
toSortedSet(): SortedSet<T>
Copies all elements into a SortedSet
 

Function Detail
source

all


 fun <T> Iterator<T>.all(predicate: (T) -> Boolean): Boolean

Returns true if all elements match the given predicate

source
val data = arrayList("foo", "bar") assertTrue { data.all{it.length == 3} } assertNot { data.all{s -> s.startsWith("b")} }
source

any


 fun <T> Iterator<T>.any(predicate: (T) -> Boolean): Boolean

Returns true if any elements match the given predicate

source
val data = arrayList("foo", "bar") assertTrue { data.any{it.startsWith("f")} } assertNot { data.any{it.startsWith("x")} }
source

appendString


 fun <T> Iterator<T>.appendString(buffer: Appendable,  [separator: String, prefix: String, postfix: String, limit: Int, truncated: String]): Unit

Appends the string from all the elements separated using the separator and using the given prefix and postfix if supplied

If a collection could be huge you can specify a non-negative value of limit which will only show a subset of the collection then it will
a special truncated separator (which defaults to “…”

source
val data = arrayList("foo", "bar") val buffer = StringBuilder() val text = data.appendString(buffer, "-", "{", "}") assertEquals("{foo-bar}", buffer.toString())
source

count


 fun <T> Iterator<T>.count(predicate: (T) -> Boolean): Int

Returns the number of elements which match the given predicate

source
val data = arrayList("foo", "bar") assertEquals(1, data.count{it.startsWith("b")}) assertEquals(2, data.count{it.size == 3})
source

dropWhileTo


 fun <T, L> Iterator<T>.dropWhileTo(result: L, predicate: (T) -> Boolean): L

Returns a list containing the everything but the first elements that satisfy the given predicate

source

filter


 fun <T> Iterator<T>.filter(predicate: (T) -> Boolean): Iterator<T>

Returns an iterator over elements which match the given predicate

source
assertEquals(arrayList(144, 233, 377, 610, 987), fibonacci().filter { it > 100 }.takeWhile { it < 1000 }.toList())
source

filterIsInstance


 fun <T, R> Iterator<T>.filterIsInstance(klass: Class<R>): Iterator<R>

Returns an iterator over elements that are instances of a given type R which is a subclass of T

source

filterNot


 fun <T> Iterator<T>.filterNot(predicate: (T) -> Boolean): Iterator<T>

Returns an iterator over elements which do not match the given predicate

source

filterNotNull


 fun <T> Iterator<T?>.filterNotNull(): Iterator<T>

Returns an iterator over non-null elements

source

filterNotNullTo


 fun <T, C> Iterator<T?>.filterNotNullTo(result: C): C

Filters all non-null elements into the given list

@includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList

source

filterNotTo


 fun <T, C> Iterator<T>.filterNotTo(result: C, predicate: (T) -> Boolean): C

Returns a list containing all elements which do not match the given predicate

@includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList

source

filterTo


 fun <T, C> Iterator<T>.filterTo(result: C, predicate: (T) -> Boolean): C

Filters all elements which match the given predicate into the given list

@includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList

source

find


 fun <T> Iterator<T>.find(predicate: (T) -> Boolean): T?

Returns the first element which matches the given predicate or null if none matched

source
val data = arrayList("foo", "bar") val x = data.find{it.startsWith("x")} assertNull(x) val f = data.find{it.startsWith("f")} f.sure() assertEquals("foo", f)
source

flatMap


 fun <T, R> Iterator<T>.flatMap(transform: (T) -> Iterator<R>): Iterator<R>

Returns an iterator over the concatenated results of transforming each element to one or more values

@includeFunctionBody ../../test/iterators/IteratorsTest.kt flatMapAndTakeExtractTheTransformedElements

source

flatMapTo


 fun <T, R> Iterator<T>.flatMapTo(result: Collection<R>, transform: (T) -> Collection<R>): Collection<R>

Returns the result of transforming each element to one or more values which are concatenated together into a single list

@includeFunctionBody ../../test/CollectionTest.kt flatMap

source

fold


 fun <T> Iterator<T>.fold(initial: T, operation: (T, T) -> T): T

Folds all elements from from left to right with the initial value to perform the operation on sequential pairs of elements

source
// lets calculate the sum of some numbers expect(10) { val numbers = arrayList(1, 2, 3, 4) numbers.fold(0){ a, b -> a + b} } expect(0) { val numbers = arrayList<Int>() numbers.fold(0){ a, b -> a + b} } // lets concatenate some strings expect("1234") { val numbers = arrayList(1, 2, 3, 4) numbers.map<Int, String>{it.toString()}.fold(""){ a, b -> a + b} }
source

foldRight


 fun <T> Iterator<T>.foldRight(initial: T, operation: (T, T) -> T): T

Folds all elements from right to left with the initial value to perform the operation on sequential pairs of elements

source
expect("1234") { val numbers = arrayList(1, 2, 3, 4) numbers.map<Int, String>{it.toString()}.foldRight(""){ a, b -> a + b} }
source

forEach


 fun <T> Iterator<T>.forEach(operation: (T) -> Unit): Unit

Performs the given operation on each element

source
val data = arrayList("foo", "bar") var count = 0 data.forEach{ count += it.length } assertEquals(6, count)
source

groupBy


 fun <T, K> Iterator<T>.groupBy(toKey: (T) -> K): Map<K, List<T>>

Groups the elements in the collection into a new Map using the supplied toKey function to calculate the key to group the elements by

source
val words = arrayList("a", "ab", "abc", "def", "abcd") val byLength = words.groupBy{ it.length } assertEquals(4, byLength.size()) val l3 = byLength.getOrElse(3, {ArrayList<String>()}) assertEquals(2, l3.size)
source

groupByTo


 fun <T, K> Iterator<T>.groupByTo(result: Map<K, List<T>>, toKey: (T) -> K): Map<K, List<T>>

Groups the elements in the collection into the given Map using the supplied toKey function to calculate the key to group the elements by

source
val words = arrayList("a", "ab", "abc", "def", "abcd") val byLength = words.groupBy{ it.length } assertEquals(4, byLength.size()) val l3 = byLength.getOrElse(3, {ArrayList<String>()}) assertEquals(2, l3.size)
source

iterator


 fun <T> Iterator<T>.iterator(): Iterator<T>

Helper to make java.util.Iterator usable in for

source

makeString


 fun <T> Iterator<T>.makeString( [separator: String, prefix: String, postfix: String, limit: Int, truncated: String]): String

Creates a string from all the elements separated using the separator and using the given prefix and postfix if supplied.

If a collection could be huge you can specify a non-negative value of limit which will only show a subset of the collection then it will
a special truncated separator (which defaults to “…”

source
val data = arrayList("foo", "bar") val text = data.makeString("-", "<", ">") assertEquals("<foo-bar>", text) val big = arrayList("a", "b", "c", "d" , "e", "f") val text2 = big.makeString(limit = 3, truncated = "*") assertEquals("a, b, c, *", text2)
source

map


 fun <T, R> Iterator<T>.map(transform: (T) -> R): Iterator<R>

Returns an iterator obtained by applying transform, a function transforming an object of type T into an object of type R

source
assertEquals(arrayList(0, 3, 3, 6, 9, 15), fibonacci().map { it * 3 }.takeWhile { (i: Int) -> i < 20 }.toList())
source

plus


 fun <T> Iterator<T>.plus(element: T): Iterator<T>

Creates an Iterator which iterates over this iterator then the given element at the end

source
val iter = arrayList("foo", "bar").iterator() val iter2 = iter + "cheese" assertEquals(arrayList("foo", "bar", "cheese"), iter2.toList()) // lets use a mutable variable var mi = arrayList("a", "b").iterator() mi += "c" assertEquals(arrayList("a", "b", "c"), mi.toList())
source

plus


 fun <T> Iterator<T>.plus(collection: Iterable<T>): Iterator<T>

Creates an Iterator which iterates over this iterator then the following collection

source
val a = arrayList("foo", "bar") val b = arrayList("cheese", "wine") val iter = a.iterator() + b.iterator() assertEquals(arrayList("foo", "bar", "cheese", "wine"), iter.toList()) // lets use a mutable variable var ml = arrayList("a").iterator() ml += a.iterator() ml += "beer" ml += b ml += "z" assertEquals(arrayList("a", "foo", "bar", "beer", "cheese", "wine", "z"), ml.toList())
source

plus


 fun <T> Iterator<T>.plus(iterator: Iterator<T>): Iterator<T>

Creates an Iterator which iterates over this iterator then the following iterator

source
val a = arrayList("foo", "bar") val b = arrayList("cheese", "wine") val iter = a.iterator() + b.iterator() assertEquals(arrayList("foo", "bar", "cheese", "wine"), iter.toList()) // lets use a mutable variable var ml = arrayList("a").iterator() ml += a.iterator() ml += "beer" ml += b ml += "z" assertEquals(arrayList("a", "foo", "bar", "beer", "cheese", "wine", "z"), ml.toList())
source

reduce


 fun <T> Iterator<T>.reduce(operation: (T, T) -> T): T

Applies binary operation to all elements of iterable, going from left to right.
Similar to fold function, but uses the first element as initial value

source
expect("1234") { val list = arrayList("1", "2", "3", "4") list.reduce { a, b -> a + b } } failsWith<UnsupportedOperationException> { arrayList<Int>().reduce { a, b -> a + b} }
source

reduceRight


 fun <T> Iterator<T>.reduceRight(operation: (T, T) -> T): T

Applies binary operation to all elements of iterable, going from right to left.
Similar to foldRight function, but uses the last element as initial value

source
expect("1234") { val list = arrayList("1", "2", "3", "4") list.reduceRight { a, b -> a + b } } failsWith<UnsupportedOperationException> { arrayList<Int>().reduceRight { a, b -> a + b} }
source

requireNoNulls


 fun <T> Iterator<T?>.requireNoNulls(): Iterator<T>

Returns an iterator containing all the non-null elements, lazily throwing an IllegalArgumentException
if there are any null elements

source
val iter = arrayList<String?>("foo", "bar").iterator() val notNull = iter.requireNoNulls() assertEquals(arrayList("foo", "bar"), notNull.toList()) val iterWithNulls = arrayList("foo", null, "bar").iterator() val notNull2 = iterWithNulls.requireNoNulls() failsWith<IllegalArgumentException> { // should throw an exception as we have a null notNull2.toList() }
source

reverse


 fun <T> Iterator<T>.reverse(): List<T>

Reverses the order the elements into a list

source
val data = arrayList("foo", "bar") val rev = data.reverse() assertEquals(arrayList("bar", "foo"), rev)
source

take


 fun <T> Iterator<T>.take(n: Int): Iterator<T>

Returns an iterator restricted to the first n elements

source
assertEquals(arrayList(0, 1, 1, 2, 3, 5, 8, 13, 21, 34), fibonacci().take(10).toList())
source

takeWhile


 fun <T> Iterator<T>.takeWhile(predicate: (T) -> Boolean): Iterator<T>

Returns an iterator restricted to the first elements that match the given predicate

source
assertEquals(arrayList(144, 233, 377, 610, 987), fibonacci().filter { it > 100 }.takeWhile { it < 1000 }.toList())
source

takeWhileTo


 fun <T, C> Iterator<T>.takeWhileTo(result: C, predicate: (T) -> Boolean): C

Returns a list containing the first elements that satisfy the given predicate

source

toCollection


 fun <T> Iterator<T>.toCollection(): Collection<T>

Copies all elements into a [[List]

source

toCollection


 fun <T, C> Iterator<T>.toCollection(result: C): C

Copies all elements into the given collection

source

toLinkedList


 fun <T> Iterator<T>.toLinkedList(): LinkedList<T>

Copies all elements into a LinkedList

source

toList


 fun <T> Iterator<T>.toList(): List<T>

Copies all elements into a List

source

toSet


 fun <T> Iterator<T>.toSet(): Set<T>

Copies all elements into a Set

source

toSortedSet


 fun <T> Iterator<T>.toSortedSet(): SortedSet<T>

Copies all elements into a SortedSet



Copyright © 2010-2012. All Rights Reserved.