https://kotlinlang.org logo
Title
b

Brian Norman

11/08/2019, 1:15 AM
In Kotlin how do you test equivalence between two Lists? i.e content is the same but not necessarily the same order?
d

Dominaezzz

11/08/2019, 1:24 AM
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

Brian Norman

11/08/2019, 1:58 AM
Thank you!
c

Can Orhan

11/08/2019, 9:11 AM
Another consideration being
listOf("a","a").size == setOf("a", "a").size // is false
c

Chris Miller

11/08/2019, 9:39 AM
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

Kroppeb

11/08/2019, 1:43 PM
If sort is to slow, you can make a hashmap with item counts