why doesnt `List` inherit from `Set` ? isn't a Set...
# announcements
l
why doesnt
List
inherit from
Set
? isn't a Set the same thing as a List but without indexing?
t
No - a set cannot have duplicates, a list can
e
A set can't contain duplicates, so no
l
oh i didn't know that. thanks
l
Both are from the
Collection
type though.
c
List
is usually ordered where as a
Set
is generally not ordered unless you use one that is specifically ordered like a
TreeSet
where the items are retrieved in sorted order.
m
Lists in Java/Kotlin/general are not sorted/ordered. Items are retrieved in the order they are inserted (assuming using the iterator). Sorting is 'on request' only.
c
My mistake in confusing ordering and sorted in this case. List will generally maintain inserted order. Set will not.
t
being pedantic - retrieved in the order they are inserted is ordered
l
i am aware of the odering stuff. i was just wondering why i can't abstract a list to a set (and the order would be just another information that gets lost with that abstraction) but sets not being able to hold duplicates already answered it
m
@tddmonkey Very true. Sorted != ordered. So in summary Lists are not sorted, but are ordered. Sets are neither sorted, nor ordered. So in this case, I wouldn't consider it pedantic 😉
t
I don’t mind being pedantic 😄
g
Kotlin default implementation is LinkedHashSet (used by buildSet, setOf, mutableSetOf, so it ordered by default
l
thats an implementation detail that could change any time
👍 1
t
If you want insertion order guarantees you should be explicit about the type you use
💯 2