@ExperimentalStdlibApi inline fun <E> buildSet( builderAction: MutableSet<E>.() -> Unit ): Set<E>
Builds a new read-only Set by populating a MutableSet using the given builderAction and returning a read-only set with the same elements.
The set passed as a receiver to the builderAction is valid only inside that function. Using it outside of the function produces an unspecified behavior.
Elements of the set are iterated in the order they were added by the builderAction.
fun main(args: Array<String>) {
//sampleStart
val x = setOf('a', 'b')
val y = buildSet(x.size + 2) {
add('b')
addAll(x)
add('c')
}
println(y) // [b, a, c]
//sampleEnd
}
@ExperimentalStdlibApi inline fun <E> buildSet( capacity: Int, builderAction: MutableSet<E>.() -> Unit ): Set<E>
Builds a new read-only Set by populating a MutableSet using the given builderAction and returning a read-only set with the same elements.
The set passed as a receiver to the builderAction is valid only inside that function. Using it outside of the function produces an unspecified behavior.
capacity is used to hint the expected number of elements added in the builderAction.
Elements of the set are iterated in the order they were added by the builderAction.
fun main(args: Array<String>) {
//sampleStart
val x = setOf('a', 'b')
val y = buildSet(x.size + 2) {
add('b')
addAll(x)
add('c')
}
println(y) // [b, a, c]
//sampleEnd
}
IllegalArgumentException
- if the given capacity is negative.
© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/build-set.html