Oh dear, Set.filter() returns a List rather than a...
# announcements
m
Oh dear, Set.filter() returns a List rather than a Set.
c
It’s because
.filter
is defined on
Iterable
. There aren’t specific overrides for each Collection type
k
You can do
set.filterTo(mutableSetOf()) { ... }
☝️ 3
m
Thanks. Ideally I'd like a SortedSet and/or SortedMutableSet, but I think I can get away with containsAll on an unsorted Set because the sets are small.
k
Well you can give it whatever collection type you want.
m
Can I compare two sets for equality (independent of order of elements)?
k
Yes,
==
does that.
m
Too easy 🙂
l
you can also use
Copy code
set.asSequence().filter(predicate).toSet()
set.asSequence().filter(predicate).toMutableSet()
set.asSequence().filter(predicate).toSortedSet()