toCollection
Appends all array elements to a mutable collection and returns it. Convenience functions toSet, toHashSet, and toMutableSet create new set instances containing only the unique elements.
Signatures
fun <T, D : Dimension, C : MutableCollection<in T>> MultiArray<T, D>.toCollection(
destination: C
): C
fun <T, D : Dimension> MultiArray<T, D>.toSet(): Set<T>
fun <T, D : Dimension> MultiArray<T, D>.toHashSet(): HashSet<T>
fun <T, D : Dimension> MultiArray<T, D>.toMutableSet(): MutableSet<T>
Parameters
Parameter | Type | Description |
|---|---|---|
|
| Target collection to append elements to. |
Returns:
toCollection: thedestinationwith all elements added.toSet/toHashSet/toMutableSet: a new set containing all unique elements.
Example
val a = mk.ndarray(mk[1, 2, 2, 3, 3, 3])
a.toSet() // {1, 2, 3}
a.toHashSet() // HashSet {1, 2, 3}
val list = mutableListOf(0)
a.toCollection(list) // [0, 1, 2, 2, 3, 3, 3]
28 February 2026