The Java collections frameworks, frustratingly, lacks any interface that indicates “an ordered collection of unique elements”, and Kotlin doesn’t step in and provide an interface/class either. So…I’m curious what people in the Kotlin community have been doing instead, when it’s important to convey to the consumers of an API that the returned collection contains only distinct values, in a deliberate iteration order, and those properties are of equal importance.
Some possibilities I can think of:
- Return a list and document the distinct values property.
- Return a set and document the iteration order. (this is what Kotlin does with
- Return a LinkedHashSet, even though it’s a concrete class and mutable (and not the
- Implement a custom wrapper class, like
class OrderedSet<T>(inner: Set<T>) : Set<T> by inner.
What do you recommend and why?