|
|||||||||||
| FRAMES NO FRAMES | |||||||||||
| SUMMARY: NESTED | PROPERTY | CONSTR | FUNCTION | DETAIL: PROPERTY | CONSTR | FUNCTION | ||||||||||
| Function Summary | ||
|---|---|---|
|
all(predicate: (T) -> Boolean): Boolean
Returns true if all elements match the given predicate |
|
|
any(predicate: (T) -> Boolean): Boolean
Returns true if any elements match the given predicate |
|
|
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 |
|
|
count(predicate: (T) -> Boolean): Int
Returns the number of elements which match the given predicate |
|
|
dropWhileTo(result: L, predicate: (T) -> Boolean): L
Returns a list containing the everything but the first elements that satisfy the given predicate |
|
|
filter(predicate: (T) -> Boolean): Iterator<T>
Returns an iterator over elements which match the given predicate |
|
|
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 |
|
|
filterNot(predicate: (T) -> Boolean): Iterator<T>
Returns an iterator over elements which do not match the given predicate |
|
|
filterNotNull(): Iterator<T>
Returns an iterator over non-null elements |
|
|
filterNotNullTo(result: C): C
Filters all non-null elements into the given list |
|
|
filterNotTo(result: C, predicate: (T) -> Boolean): C
Returns a list containing all elements which do not match the given predicate |
|
|
filterTo(result: C, predicate: (T) -> Boolean): C
Filters all elements which match the given predicate into the given list |
|
|
find(predicate: (T) -> Boolean): T?
Returns the first element which matches the given predicate or null if none matched |
|
|
flatMap(transform: (T) -> Iterator<R>): Iterator<R>
Returns an iterator over the concatenated results of transforming each element to one or more values |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
forEach(operation: (T) -> Unit): Unit
Performs the given operation on each element |
|
|
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 |
|
|
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 |
|
|
iterator(): Iterator<T>
Helper to make java.util.Iterator usable in for |
|
|
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. |
|
|
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 |
|
|
plus(element: T): Iterator<T>
Creates an Iterator which iterates over this iterator then the given element at the end |
|
|
plus(collection: Iterable<T>): Iterator<T>
Creates an Iterator which iterates over this iterator then the following collection |
|
|
plus(iterator: Iterator<T>): Iterator<T>
Creates an Iterator which iterates over this iterator then the following iterator |
|
|
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 |
|
|
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 |
|
|
requireNoNulls(): Iterator<T>
Returns an iterator containing all the non-null elements, lazily throwing an IllegalArgumentException if there are any null elements |
|
|
reverse(): List<T>
Reverses the order the elements into a list |
|
|
take(n: Int): Iterator<T>
Returns an iterator restricted to the first n elements |
|
|
takeWhile(predicate: (T) -> Boolean): Iterator<T>
Returns an iterator restricted to the first elements that match the given predicate |
|
|
takeWhileTo(result: C, predicate: (T) -> Boolean): C
Returns a list containing the first elements that satisfy the given predicate |
|
|
toCollection(): Collection<T>
Copies all elements into a [[List] |
|
|
toCollection(result: C): C
Copies all elements into the given collection |
|
|
toLinkedList(): LinkedList<T>
Copies all elements into a LinkedList |
|
|
toList(): List<T>
Copies all elements into a List |
|
|
toSet(): Set<T>
Copies all elements into a Set |
|
|
toSortedSet(): SortedSet<T>
Copies all elements into a SortedSet |
|
| Function Detail |
|---|
fun <T> Iterator<T>.all(predicate: (T) -> Boolean): Boolean
Returns true if all elements match the given predicate
val data = arrayList("foo", "bar")
assertTrue {
data.all{it.length == 3}
}
assertNot {
data.all{s -> s.startsWith("b")}
}
fun <T> Iterator<T>.any(predicate: (T) -> Boolean): Boolean
Returns true if any elements match the given predicate
val data = arrayList("foo", "bar")
assertTrue {
data.any{it.startsWith("f")}
}
assertNot {
data.any{it.startsWith("x")}
}
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 “…”
val data = arrayList("foo", "bar")
val buffer = StringBuilder()
val text = data.appendString(buffer, "-", "{", "}")
assertEquals("{foo-bar}", buffer.toString())
fun <T> Iterator<T>.count(predicate: (T) -> Boolean): Int
Returns the number of elements which match the given predicate
val data = arrayList("foo", "bar")
assertEquals(1, data.count{it.startsWith("b")})
assertEquals(2, data.count{it.size == 3})
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
assertEquals(arrayList(144, 233, 377, 610, 987), fibonacci().filter { it > 100 }.takeWhile { it < 1000 }.toList())
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
@includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
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
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
fun <T> Iterator<T>.find(predicate: (T) -> Boolean): T?
Returns the first element which matches the given predicate or null if none matched
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)
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
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
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
// 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}
}
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
expect("1234") {
val numbers = arrayList(1, 2, 3, 4)
numbers.map<Int, String>{it.toString()}.foldRight(""){ a, b -> a + b}
}
fun <T> Iterator<T>.forEach(operation: (T) -> Unit): Unit
Performs the given operation on each element
val data = arrayList("foo", "bar")
var count = 0
data.forEach{ count += it.length }
assertEquals(6, count)
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
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)
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
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)
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.
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 “…”
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)
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
assertEquals(arrayList(0, 3, 3, 6, 9, 15), fibonacci().map { it * 3 }.takeWhile { (i: Int) -> i < 20 }.toList())
fun <T> Iterator<T>.plus(element: T): Iterator<T>
Creates an Iterator which iterates over this iterator then the given element at the end
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())
fun <T> Iterator<T>.plus(collection: Iterable<T>): Iterator<T>
Creates an Iterator which iterates over this iterator then the following collection
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())
fun <T> Iterator<T>.plus(iterator: Iterator<T>): Iterator<T>
Creates an Iterator which iterates over this iterator then the following iterator
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())
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
expect("1234") {
val list = arrayList("1", "2", "3", "4")
list.reduce { a, b -> a + b }
}
failsWith<UnsupportedOperationException> {
arrayList<Int>().reduce { a, b -> a + b}
}
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
expect("1234") {
val list = arrayList("1", "2", "3", "4")
list.reduceRight { a, b -> a + b }
}
failsWith<UnsupportedOperationException> {
arrayList<Int>().reduceRight { a, b -> a + b}
}
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
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()
}
fun <T> Iterator<T>.reverse(): List<T>
Reverses the order the elements into a list
val data = arrayList("foo", "bar")
val rev = data.reverse()
assertEquals(arrayList("bar", "foo"), rev)
fun <T> Iterator<T>.take(n: Int): Iterator<T>
Returns an iterator restricted to the first n elements
assertEquals(arrayList(0, 1, 1, 2, 3, 5, 8, 13, 21, 34), fibonacci().take(10).toList())
fun <T> Iterator<T>.takeWhile(predicate: (T) -> Boolean): Iterator<T>
Returns an iterator restricted to the first elements that match the given predicate
assertEquals(arrayList(144, 233, 377, 610, 987), fibonacci().filter { it > 100 }.takeWhile { it < 1000 }.toList())
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>.toSortedSet(): SortedSet<T>
Copies all elements into a SortedSet
|
||||||||||
| FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | PROPERTY | CONSTR | FUNCTION | DETAIL: PROPERTY | CONSTR | FUNCTION | |||||||||