Skip to content

Set

Type

The Set<> type is used to represent variables that are collections of values of the same type without duplicates. The specified type indicates the type of the elements of the set, e.g. Set<Number> is a set of unique Number values.

Conceptually, a basic Set provides no ordering, however, the implementation of Set guarantees deterministic behavior in that it maintains insertion order. This means that the same Set, built up in the same order, will always have the same iteration order. This makes it easier to compare multiple runs of the same logic involving sets (which can be convenient when writing tests). However, this is not behavior that you should take advantage of for the correct functioning of your logic - if you need an ordered data structure then you should use something like a List.

Sets are instantiated using setOf().

Methods

filter

<function> Returns a copy of the set with the elements filtered according to the predicate. The original set is not modified.

Receiver

Set<T>

Type Arguments

T - the element type

Arguments

predicate - (T) -> Boolean - the filter function applied to each element of the set

Returns

Set<T> - a copy of the set containing those elements of the set for which the filter function returns true

Usage

setOf<Number>().filter(function(x: Number) -> x < 3) == setOf<Number>()
setOf(4, 3, 2, 1).filter(function(x: Number) -> x < 3) == setOf(1, 2)

plus

<function> The infix operator + (or the method plus) returns a set concatenating the right hand and left hand sets, removing duplicates.

Receiver

Set<T>

Type Arguments

T - the element type

Arguments

other - Set<T>

Returns

Set<T>

Usage

setOf<Number>() + setOf(4, 3, 2, 1) == setOf(1, 2, 3, 4)
setOf<Number>().plus(setOf(4, 3, 2, 1)) == setOf(1, 2, 3, 4)
setOf(4, 3, 2, 1) + setOf<Number>() == setOf(1, 2, 3, 4)
setOf(4, 3, 2, 1) + setOf(5, 6) + setOf(3, 3) == setOf(1, 2, 3, 4, 5, 6)

toList

<function> Converts a set into a list with the same values.

Receiver

Set<T>

Type Arguments

T - the element type

Returns

List<T> - a list of the values in the set, sorted by their insertion order to the set.

Usage
setOf(2, 3, 1).toList().sort() == listOf(1, 2, 3)

with

<function> Returns a copy of the set with the provided element added, if not already present. The original set is not modified.

Receiver

Set<T>

Type Arguments

T - the element type

Arguments

element - T - the element to be added

Returns

Set<T> - a copy of the set with the element added

Usage

setOf(4, 3, 2, 1).with(5) == setOf(1, 2, 3, 4, 5)
setOf<Number>().with(7) == setOf(7)
setOf(4, 3, 2, 1).with(2) == setOf(1, 2, 3, 4)

without

<function> Returns a copy of the set with the provided element removed, if present. The original set is not modified.

Receiver

Set<T>

Type Arguments

T - the element type

Arguments

element - T - the element to be removed

Returns

Set<T> - a copy of the set without the given element

Usage

setOf(4, 3, 2, 1).without(5) == setOf(1, 2, 3, 4)
setOf(4, 3, 2, 1, 4).without(4) == setOf(1, 2, 3)

takeFirst

<function> Takes the first n items of this Set. If n is larger than the Set's size, then the entire collection is returned. Otherwise, the first n elements are returned.

Receiver

Set<T>

Type Arguments

T - the element type

Arguments

n - Number - the number of items to take, must be greater than or equal to 0

Returns

Set<T> - the first n values in the Set, sorted by their original insertion order.

Usage
setOf(2, 3, 1).takeFirst(2)

takeLast

<function> Takes the last n items of this Set. If n is larger than the Set's size, then the entire collection is returned. Otherwise, the last n elements are returned.

Receiver

Set<T>

Type Arguments

T - the element type

Arguments

n - Number - the number of items to take, must be greater than or equal to 0

Returns

Set<T> - the last n values in the Set, sorted by their original insertion order.

Usage
setOf(2, 3, 1).takeLast(2)

Inherited methods

allMatch

<function> Determines whether the predicate is true for all elements in the collection. allMatch on an empty collection returns true.

Receiver

Collection<T>

Type Arguments

T - the element type

Arguments

predicate - (T) -> Boolean - The predicate tested on the elements of the collection

Returns

Boolean - true if predicate returns true for all elements in the collection, false otherwise

Usage

setOf<Number>().allMatch(function(x: Number) -> x < 3) == true
setOf(4, 3, 2, 1).allMatch(function(x: Number) -> x < 3) == false
setOf(2, 1).allMatch(function(x: Number) -> x < 3) == true

anyMatch

<function> Determines whether the predicate is true for at least one element in the collection. anyMatch on an empty collection returns false.

Receiver

Collection<T>

Type Arguments

T - the element type

Arguments

predicate - (T) -> Boolean - The predicate tested on the elements of the collection

Returns

Boolean - true if predicate returns true for any element in the collection, false otherwise

Usage

setOf<Number>().anyMatch(function(x: Number) -> x < 3) == false
setOf(4, 3, 2, 1).anyMatch(function(x: Number) -> x < 3) == true
setOf(4, 3).anyMatch(function(x: Number) -> x < 3) == false

asList

<function> Converts a collection into a list with the same values, if needed. It creates a new List when the collection is a Set. It is a no-op when the collection is a List.

Receiver

Collection<T>

Type Arguments

T - the element type

Returns

List<T> - a list of the values in the collection, sorted by their original insertion order.

Usage
setOf(2, 3, 1).asList() == listOf(2, 3, 1)

contains

<function> Determines whether the collection contains the given element.

Receiver

Collection<T>

Type Arguments

T - the element type

Arguments

element - T - The element to be searched in the collection.

Returns

Boolean - true if element is in the collection, false otherwise

Usage

setOf<Number>().contains(5) == false
setOf(4, 3, 2, 1).contains(3) == true
setOf(4, 3, 2, 1).contains(5) == false

flatMap

<function> Applies the given function to each element of the original collection, producing a collection of U for each, and then build a result list of U from the elements of these collections. Note that, irrespective of whether the function passed to flatMap returns a set or a list, the result of flatMap is always a list.

Receiver

Collection<T>

Type Arguments

T - the element type

U - the result type

Arguments

transformer - (T) -> Collection<U> - A function transforming each element of the original collection into a collection of results.

Returns

List<U> - a list of all elements returned by the transformer function

Usage

setOf(1, 2, 3).flatMap(function(x: Number) -> listOf(x, x * x)) == listOf(1, 1, 2, 4, 3, 9)
setOf(1, 2, 3).flatMap(function(x: Number) -> setOf(x, x * x)) == listOf(1, 2, 4, 3, 9)

flatten

<function> When applied to a collection whose elements are themselves collections, returns a list where the elements are the elements of those collections. Attempting to apply flatten() to a collection whose elements are not collections will result in a compile time error.

Receiver

Collection<Collection<T>>

Type Arguments

T - the element type

Returns

List<T> - list of all elements contained in elements of the original collection

Usage

setOf(listOf(1, 2), listOf(3, 4, 5)).flatten() == listOf(1, 2, 3, 4, 5)
setOf(setOf(1, 2), setOf(3, 4, 5)).flatten() == listOf(1, 2, 3, 4, 5)

fold

<function> Folds collection items into an accumulated value, starting with the initial value as its accumulator. It requires a lambda with two inputs, the first of which matches the type of the initial, and the second matches the type of the collection elements.

Receiver

Collection<T>

Type Arguments

T - the element type

U - the result type

Arguments

initval - U - the initial value of the accumulator

transformer - (U, T) -> U - the transforming function that updates the accumulator with a new value.

Returns

U - the value of accumulator after applying the transformer to each element of the collection

Usage

setOf(9, 10, 1).fold(0, function(acc: Number, elem: Number) returns Number -> acc + elem)
== 20 // equivalent to sum()
setOf(9, 10, 1).fold("", function(acc: Text, elem: Number) returns Text -> acc + elem.toText())
== "9101"

forEach

<function> Applies a function to each element of a collection in the collection's order. Ignores the return value of the action.

Receiver

Collection<T>

Type Arguments

T - the element type

U - the result type (ignored)

Arguments

action - (T) -> U - the function to call on each element

Usage
protocol[p] SomeProtocol() {
    var localSet: Set<Number> = setOf<Number>();

    permission[p] f(inputs: Set<Number>)  {
        inputs.forEach(function(n: Number) returns Unit -> {
            localSet = localSet.with(n * 2);
        });
    };
};

isEmpty

<function> Determines if the collection contains no elements.

Receiver

Collection<T>

Type Arguments

T - the element type

Returns

Boolean - true if the size of the collection is 0, otherwise returns false.

Usage

setOf(4, 3, 2, 1).isEmpty() == false
setOf<Number>().isEmpty() == true

isNotEmpty

<function> Determines if the collection contains any elements.

Receiver

Collection<T>

Type Arguments

T - the element type

Returns

Boolean - false if the size of the collection is 0, otherwise returns true.

Usage

setOf(4, 3, 2, 1).isNotEmpty() == true
setOf<Number>().isNotEmpty() == false

map

<function> Applies a transformation function on each element of the collection and returns a list of results.

Receiver

Collection<T>

Type Arguments

T - the element type

U - the return type of the transformer

Arguments

transformer - (T) -> U - the transformation function applied to each element

Returns

List<U> - a list where the elements of the original collection are converted using transformer

Usage

setOf<Number>().map(function(x: Number) -> (2*x - 0.5).roundTo(0)) == listOf<Number>()
setOf(4.0, 3.6, 2.2, 1.1).map(function(x: Number) -> (2*x - 0.5).roundTo(0)).toSet() == setOf(8, 7, 4, 2)

noneMatch

<function> Determines whether the predicate is false for all elements in the collection. noneMatch on an empty collection returns true.

Receiver

Collection<T>

Type Arguments

T - the element type

Arguments

predicate - (T) -> Boolean - The predicate tested on the elements of the collection

Returns

Boolean - true if predicate returns false for all elements in the collection, false otherwise

Usage

setOf<Number>().noneMatch(function(x: Number) -> x < 3) == true
setOf(5, 4).noneMatch(function(x: Number) -> x < 3) == true
setOf(2, 1).noneMatch(function(x: Number) -> x < 3) == false

size

<function> Gets the size of the collection.

Receiver

Collection<T>

Type Arguments

T - the element type

Returns

Number - the number of elements in the collection

Usage

setOf<Number>().size() == 0
setOf(4, 3, 2, 1).size() == 4

sum

<function> Returns the sum of all numbers in the collection. sum() applied to an empty collection returns 0.

Receiver

Collection<Number>

Returns

Number - the total of all numbers in the collection

Usage

setOf<Number>().sum() == 0
setOf(4, 3, 2, 1).sum() == 10
setOf(4.0, 3.0, 2.0, 1.1).sum() == 10.1