September 2019
Intermediate to advanced
462 pages
11h 3m
English
Sets are unordered collections of elements. Like the methods for creating List<T>, which has both immutable/read-only and mutable/read-write versions, you may create instances of Set<T> using setOf() or instances of MutableSet<T> using mutableSetOf(). You may also use hashSetOf() to get a reference of type java.util.HashSet<T>: linkedSetOf() for LinkedHashSet, and sortedSetOf() for TreeSet<T>.
Here’s a set of fruits, with a duplicate element:
| | val fruits: Set<String> = setOf("Apple", "Banana", "Apple") |
Since sets guarantee uniqueness of elements, the duplicate is discarded in the set created:
| | println(fruits) //[Apple, Banana] |
The instance created by setOf() is of the type Set<T> interface, ...
Read now
Unlock full access