In Kotlin how do you test equivalence between two ...
# announcements
b
In Kotlin how do you test equivalence between two Lists? i.e content is the same but not necessarily the same order?
d
If order is not important then what you describe is Set equivalence.
list1.toSet() == list2.toSet()
.
Consider using
LinkedHashSet
if you need a set of values in insertion order.
b
Thank you!
c
Another consideration being
listOf("a","a").size == setOf("a", "a").size // is false
c
Using a set and size would still fail for listOf("a", "a", "b") vs listOf("a", "b", "b")
😮 1
Sort + compare might be more appropriate depending on your use case
🙏 1
k
If sort is to slow, you can make a hashmap with item counts