https://kotlinlang.org logo
Title
m

mathew murphy

07/18/2019, 9:06 PM
Oh dear, Set.filter() returns a List rather than a Set.
k

karelpeeters

07/18/2019, 9:08 PM
You can do
set.filterTo(mutableSetOf()) { ... }
☝️ 3
c

Casey Brooks

07/18/2019, 9:08 PM
It’s because
.filter
is defined on
Iterable
. There aren’t specific overrides for each Collection type
m

mathew murphy

07/18/2019, 9:11 PM
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

karelpeeters

07/18/2019, 9:12 PM
Well you can give it whatever collection type you want.
m

mathew murphy

07/18/2019, 9:25 PM
Can I compare two sets for equality (independent of order of elements)?
k

karelpeeters

07/18/2019, 9:25 PM
Yes,
==
does that.
m

mathew murphy

07/18/2019, 9:34 PM
Too easy 🙂
l

LS

07/18/2019, 9:38 PM
you can also use
set.asSequence().filter(predicate).toSet()
set.asSequence().filter(predicate).toMutableSet()
set.asSequence().filter(predicate).toSortedSet()