October 2018
Intermediate to advanced
370 pages
9h 15m
English
We can create an immutable set using the setOf keyword. The set element is inherited from the collection interface, which means that it is the immutable type collection that provides read-only functionality. Create a set and add some duplicate values in it:
val setItems = setOf(1,1,2,3,3,4,5,5)
Now, check the size of this collection. As mentioned earlier, sets don't support duplicate elements, so the size of the collection will be 5 instead of 8:
println("Set size ${setItems.size}")
Sets don't contain their own functions, but they do override all functions from the collection interface. We can check the presence of any element by using the contains function:
var element = 5var result = setItems.contains(element)println("Set ...Read now
Unlock full access