Marcin Wisniowski
06/25/2025, 1:40 PMlistOf(1, 2, 3).containsAll(listOf(2, 2))
returns true. What's the best alternative that will tell me correctly that the first list doesn't, in fact, contain all the elements of the second list? I want to consider the amount of items.Marcin Wisniowski
06/25/2025, 1:46 PMlistOf(2, 2).toMutableList().also { list -> listOf(1, 2, 3).forEach { list.remove(it) }.isEmpty()
, but it seems a bit much.Joffrey
06/25/2025, 1:47 PMMarcin Wisniowski
06/25/2025, 1:48 PMMarcin Wisniowski
06/25/2025, 1:50 PMJoffrey
06/25/2025, 6:24 PMMarcin Wisniowski
06/25/2025, 6:34 PMcontainsAll
, my problem was with the minus
operator. Which has the same problem, but shows my actual intent.
My original code was:
val missingItems = expectedItems - currentItems
val extraItems = currentItems - expectedItems
This had the problem, that the minus
operator does not consider counts, so this didn't behave as expected when expected/current items had duplicates, which is a valid state for them to be in.Marcin Wisniowski
06/25/2025, 6:34 PMMarcin Wisniowski
06/25/2025, 6:37 PMJoffrey
06/25/2025, 6:43 PMMarcin Wisniowski
06/25/2025, 6:52 PMmissingItems
and extraItems
are both empty, but the two lists are not equal, then they must be in different order (but I don't actually care where).Klitos Kyriacou
06/26/2025, 10:48 AM