kotlin
Extensions on java.util.Collection

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

Property Summary
val
empty: Boolean
           
val
indices: IntRange
           
 

Function Summary
fun <T>
Iterable<T>
all(predicate: (T) -> Boolean): Boolean
Returns true if all elements match the given predicate
fun <T>
Iterable<T>
any(predicate: (T) -> Boolean): Boolean
Returns true if any elements match the given predicate
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
fun <T>
Iterable<T>
contains(item: T): Boolean
Checks if collection contains given item.
fun <T>
Iterable<T>
count(): Int
Count the number of elements in collection.
fun <T>
Iterable<T>
count(predicate: (T) -> Boolean): Int
Returns the number of elements which match the given predicate
fun <T>
Iterable<T>
drop(n: Int): List<T>
Returns a list containing everything but the first n elements
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
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
fun <T>
Iterable<T>
filterNot(predicate: (T) -> Boolean): List<T>
Returns a list containing all elements which do not match the given predicate
fun <T>
Iterable<T?>
filterNotNull(): List<T>
Returns a list containing all the non-null elements
fun <T, C>
Iterable<T?>
filterNotNullTo(result: C): C
Filters all non-null elements into the given list
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
fun <T, C>
Iterable<T>
filterTo(result: C, predicate: (T) -> Boolean): C
Filters all elements which match the given predicate into the given list
fun <T>
Iterable<T>
find(predicate: (T) -> Boolean): T?
Returns the first element which matches the given predicate or null if none matched
fun <T>
Iterable<T>
first(): T
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
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
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
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
fun <T>
Iterable<T>
forEach(operation: (T) -> Unit): Unit
Performs the given operation on each element
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
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
fun <T>
Iterable<T>
last(): T
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.
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
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>
notEmpty(): Boolean
Returns true if the collection is not empty
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
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
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
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
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
fun <T>
Iterable<T>
reverse(): List<T>
Reverses the order the elements into a list
fun <T>
Iterable<T>
sort(): List<T>
fun <T>
Iterable<T>
sort(comparator: Comparator<T>): List<T>
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)
fun <T>
Iterable<T>
take(n: Int): List<T>
Returns a list containing the first n elements
fun <T>
Iterable<T>
takeWhile(predicate: (T) -> Boolean): List<T>
Returns a list containing the first elements that satisfy the given predicate
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>
Collection<T>
toArray(): Array<T>
Converts the collection to an array
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>
toList(): List<T>
Copies all elements into a List
fun <T>
Iterable<T>
toSet(): Set<T>
Copies all elements into a Set
fun <T>
Iterable<T>
toSortedList(): List<T>
TODO these functions don’t work when they generate the Array versions when they are in JLIterables
fun <T>
Iterable<T>
toSortedList(comparator: Comparator<T>): List<T>
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
 

Function Detail
source

all


 fun <T> Iterable<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> Iterable<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> 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 “…”

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

contains


 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.

source

count


 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.

source

count


 fun <T> Iterable<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

drop


 fun <T> Iterable<T>.drop(n: Int): List<T>

Returns a list containing everything but the first n elements

source
val coll = arrayList("foo", "bar", "abc") assertEquals(arrayList("bar", "abc"), coll.drop(1)) assertEquals(arrayList("abc"), coll.drop(2))
source

dropWhile


 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

source
val coll = arrayList("foo", "bar", "abc") assertEquals(arrayList("bar", "abc"), coll.dropWhile{ it.startsWith("f") })
source

dropWhileTo


 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

source

filter


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

Returns a list containing all elements which match the given predicate

source
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)
source

filterNot


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

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

source
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)
source

filterNotNull


 fun <T> Iterable<T?>.filterNotNull(): List<T>

Returns a list containing all the non-null elements

source
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> }
source

filterNotNullTo


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

Filters all non-null elements into the given list

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

source

filterNotTo


 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

source

filterTo


 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

source

find


 fun <T> Iterable<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

first


 fun <T> Iterable<T>.first(): T
source

flatMap


 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

source

flatMapTo


 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

source

fold


 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

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> 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

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> Iterable<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> 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

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> 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

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

last


 fun <T> Iterable<T>.last(): T
source

makeString


 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 “…”

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> 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

source
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)
source

mapTo


 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

source

notEmpty


 fun <T> Collection<T>.notEmpty(): Boolean

Returns true if the collection is not empty

source

orEmpty


 fun <T> Collection<T>.orEmpty(): Collection<T>

Returns the Collection if its not null otherwise it returns the empty list

source

plus


 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

source
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)
source

plus


 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

source
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)
source

reduce


 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

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> 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

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> Iterable<T?>.requireNoNulls(): List<T>

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

source
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() }
source

reverse


 fun <T> Iterable<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

sort


 fun <T> Iterable<T>.sort(): List<T>
source

sort


 fun <T> Iterable<T>.sort(comparator: Comparator<T>): List<T>
source

sortBy


 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

source

take


 fun <T> Iterable<T>.take(n: Int): List<T>

Returns a list containing the first n elements

source
val coll = arrayList("foo", "bar", "abc") assertEquals(arrayList("foo"), coll.take(1)) assertEquals(arrayList("foo", "bar"), coll.take(2))
source

takeWhile


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

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

source
val coll = arrayList("foo", "bar", "abc") assertEquals(arrayList("foo"), coll.takeWhile{ it.startsWith("f") }) assertEquals(arrayList("foo", "bar", "abc"), coll.takeWhile{ it.size == 3 })
source

takeWhileTo


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

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

source

toArray


 fun <T> Collection<T>.toArray(): Array<T>

Converts the collection to an array

source

toCollection


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

Copies all elements into a [[List]

source

toCollection


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

Copies all elements into the given collection

source

toLinkedList


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

Copies all elements into a LinkedList

source

toList


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

Copies all elements into a List

source

toSet


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

Copies all elements into a Set

source

toSortedList


 fun <T> Iterable<T>.toSortedList(): List<T>

TODO these functions don’t work when they generate the Array versions when they are in JLIterables

source

toSortedList


 fun <T> Iterable<T>.toSortedList(comparator: Comparator<T>): List<T>
source

toSortedSet


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

Copies all elements into a SortedSet

source

withIndices


 fun <T> Iterable<T>.withIndices(): List<#(Int, T)>

Convert collection of arbitrary elements to a List of tuples of the index and the element

source
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)


Copyright © 2010-2012. All Rights Reserved.