|
|||||||||||
| FRAMES NO FRAMES | |||||||||||
| SUMMARY: NESTED | PROPERTY | CONSTR | FUNCTION | DETAIL: PROPERTY | CONSTR | FUNCTION | ||||||||||
| Property Summary | |
|---|---|
val |
|
val |
|
| 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 |
|
|
contains(item: T): Boolean
Checks if collection contains given item. |
|
|
count(): Int
Count the number of elements in collection. |
|
|
count(predicate: (T) -> Boolean): Int
Returns the number of elements which match the given predicate |
|
|
drop(n: Int): List<T>
Returns a list containing everything but the first n elements |
|
|
dropWhile(predicate: (T) -> Boolean): List<T>
Returns a list containing the everything but the first elements that satisfy 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): List<T>
Returns a list containing all elements which match the given predicate |
|
|
filterNot(predicate: (T) -> Boolean): List<T>
Returns a list containing all elements which do not match the given predicate |
|
|
filterNotNull(): List<T>
Returns a list containing all the 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 |
|
|
first(): T
|
|
|
flatMap(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 collection |
|
|
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 |
|
|
last(): 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. |
|
|
map(transform: (T) -> R): List<R>
Returns a new List containing the results of applying the given transform function to each element in this collection |
|
|
mapTo(result: C, transform: (T) -> R): C
Transforms each element of this collection with the given transform function and adds each return value to the given results collection |
|
|
notEmpty(): Boolean
Returns true if the collection is not empty |
|
|
orEmpty(): Collection<T>
Returns the Collection if its not null otherwise it returns the empty list |
|
|
plus(element: T): List<T>
Creates a copy of this collection as a List with the element added at the end |
|
|
plus(elements: Iterable<T>): List<T>
Creates a copy of this collection as a List with all the elements added at the end |
|
|
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(): List<T>
Returns a list containing all the non-null elements, throwing an IllegalArgumentException if there are any null elements |
|
|
reverse(): List<T>
Reverses the order the elements into a list |
|
|
sort(): List<T>
|
|
|
sort(comparator: Comparator<T>): List<T>
|
|
|
sortBy(f: (T) -> R): List<T>
Copies all elements into a List and sorts it by value of compare_function(element) |
|
|
take(n: Int): List<T>
Returns a list containing the first n elements |
|
|
takeWhile(predicate: (T) -> Boolean): List<T>
Returns a list containing the first elements that satisfy the given predicate |
|
|
takeWhileTo(result: C, predicate: (T) -> Boolean): C
Returns a list containing the first elements that satisfy the given predicate |
|
|
toArray(): Array<T>
Converts the collection to an array |
|
|
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 |
|
|
toSortedList(): List<T>
TODO these functions don’t work when they generate the Array |
|
|
toSortedList(comparator: Comparator<T>): List<T>
|
|
|
toSortedSet(): SortedSet<T>
Copies all elements into a SortedSet |
|
|
withIndices(): List<#(Int, T)>
Convert collection of arbitrary elements to a List of tuples of the index and the element |
|
| Function Detail |
|---|
fun <T> Iterable<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> Iterable<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> Iterable<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> Iterable<T>.contains(item: T): Boolean
Checks if collection contains given item.
Method checks equality of the objects with T.equals method.
If collection implements java.util.AbstractCollection an overridden implementation of the contains
method will be used.
fun <T> Iterable<T>.count(): Int
Count the number of elements in collection.
If base collection implements Collection interface method Collection.size() will be used.
Otherwise, this method determines the count by iterating through the all items.
fun <T> Iterable<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> Iterable<T>.drop(n: Int): List<T>
Returns a list containing everything but the first n elements
val coll = arrayList("foo", "bar", "abc")
assertEquals(arrayList("bar", "abc"), coll.drop(1))
assertEquals(arrayList("abc"), coll.drop(2))
fun <T> Iterable<T>.dropWhile(predicate: (T) -> Boolean): List<T>
Returns a list containing the everything but the first elements that satisfy the given predicate
val coll = arrayList("foo", "bar", "abc")
assertEquals(arrayList("bar", "abc"), coll.dropWhile{ it.startsWith("f") })
fun <T, L> Iterable<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> Iterable<T>.filter(predicate: (T) -> Boolean): List<T>
Returns a list containing all elements which match the given predicate
val data = arrayList("foo", "bar")
val foo = data.filter{it.startsWith("f")}
assertTrue {
foo.all{it.startsWith("f")}
}
assertEquals(1, foo.size)
assertEquals(arrayList("foo"), foo)
fun <T> Iterable<T>.filterNot(predicate: (T) -> Boolean): List<T>
Returns a list containing all elements which do not match the given predicate
val data = arrayList("foo", "bar")
val foo = data.filterNot{it.startsWith("b")}
assertTrue {
foo.all{it.startsWith("f")}
}
assertEquals(1, foo.size)
assertEquals(arrayList("foo"), foo)
fun <T> Iterable<T?>.filterNotNull(): List<T>
Returns a list containing all the non-null elements
val data = arrayList(null, "foo", null, "bar")
val foo = data.filterNotNull()
assertEquals(2, foo.size)
assertEquals(arrayList("foo", "bar"), foo)
assertTrue {
foo is List<String>
}
fun <T, C> Iterable<T?>.filterNotNullTo(result: C): C
Filters all non-null elements into the given list
@includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
fun <T, C> Iterable<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> Iterable<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> Iterable<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> Iterable<T>.flatMap(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 collection
@includeFunctionBody ../../test/CollectionTest.kt flatMap
fun <T, R> Iterable<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> Iterable<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> Iterable<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> Iterable<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> Iterable<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> Iterable<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> Iterable<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> Collection<T>.map(transform: (T) -> R): List<R>
Returns a new List containing the results of applying the given transform function to each element in this collection
val data = arrayList("foo", "bar")
val lengths = data.map<String, Int>{ it.length }
assertTrue {
lengths.all{it == 3}
}
assertEquals(2, lengths.size)
assertEquals(arrayList(3, 3), lengths)
fun <T, R, C> Collection<T>.mapTo(result: C, transform: (T) -> R): C
Transforms each element of this collection with the given transform function and
adds each return value to the given results collection
fun <T> Collection<T>.orEmpty(): Collection<T>
Returns the Collection if its not null otherwise it returns the empty list
fun <T> Iterable<T>.plus(element: T): List<T>
Creates a copy of this collection as a List with the element added at the end
val list = arrayList("foo", "bar")
val list2 = list + "cheese"
assertEquals(arrayList("foo", "bar"), list)
assertEquals(arrayList("foo", "bar", "cheese"), list2)
// lets use a mutable variable
var list3 = arrayList("a", "b")
list3 += "c"
assertEquals(arrayList("a", "b", "c"), list3)
fun <T> Iterable<T>.plus(elements: Iterable<T>): List<T>
Creates a copy of this collection as a List with all the elements added at the end
val a = arrayList("foo", "bar")
val b = arrayList("cheese", "wine")
val list = a + b
assertEquals(arrayList("foo", "bar", "cheese", "wine"), list)
// lets use a mutable variable
var ml = a
ml += "beer"
ml += b
ml += "z"
assertEquals(arrayList("foo", "bar", "beer", "cheese", "wine", "z"), ml)
fun <T> Iterable<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> Iterable<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> Iterable<T?>.requireNoNulls(): List<T>
Returns a list containing all the non-null elements, throwing an IllegalArgumentException if there are any null elements
val data = arrayList<String?>("foo", "bar")
val notNull = data.requireNoNulls()
assertEquals(arrayList("foo", "bar"), notNull)
val hasNulls = arrayList("foo", null, "bar")
failsWith<IllegalArgumentException> {
// should throw an exception as we have a null
hasNulls.requireNoNulls()
}
fun <T> Iterable<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, R> Iterable<T>.sortBy(f: (T) -> R): List<T>
Copies all elements into a List and sorts it by value of compare_function(element)
E.g. arrayList(“two” to 2, “one” to 1).sortBy({it._2}) returns list sorted by second element of tuple
@includeFunctionBody ../../test/CollectionTest.kt sortBy
fun <T> Iterable<T>.take(n: Int): List<T>
Returns a list containing the first n elements
val coll = arrayList("foo", "bar", "abc")
assertEquals(arrayList("foo"), coll.take(1))
assertEquals(arrayList("foo", "bar"), coll.take(2))
fun <T> Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T>
Returns a list containing the first elements that satisfy the given predicate
val coll = arrayList("foo", "bar", "abc")
assertEquals(arrayList("foo"), coll.takeWhile{ it.startsWith("f") })
assertEquals(arrayList("foo", "bar", "abc"), coll.takeWhile{ it.size == 3 })
fun <T, C> Iterable<T>.takeWhileTo(result: C, predicate: (T) -> Boolean): C
Returns a list containing the first elements that satisfy the given predicate
fun <T> Iterable<T>.toCollection(): Collection<T>
Copies all elements into a [[List]
fun <T, C> Iterable<T>.toCollection(result: C): C
Copies all elements into the given collection
fun <T> Iterable<T>.toLinkedList(): LinkedList<T>
Copies all elements into a LinkedList
fun <T> Iterable<T>.toSortedList(): List<T>
TODO these functions don’t work when they generate the Array
fun <T> Iterable<T>.toSortedSet(): SortedSet<T>
Copies all elements into a SortedSet
fun <T> Iterable<T>.withIndices(): List<#(Int, T)>
Convert collection of arbitrary elements to a List of tuples of the index and the element
val data = arrayList("foo", "bar")
val wis = data.withIndices()
var index = 0
for (withIndex in wis) {
assertEquals(withIndex._1, index)
assertEquals(withIndex._2, data[index])
index++
}
assertEquals(data.size(), index)
|
||||||||||
| FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | PROPERTY | CONSTR | FUNCTION | DETAIL: PROPERTY | CONSTR | FUNCTION | |||||||||