0

I'm trying to make a filtering in a list by another list of Values Ex

I want to make this

  selectedAreasList = ListOf(1,2,3)

  initialItemProductList.filter { it.areaId in setOf(selectedAreasList) }

instead

  initialItemProductList.filter { it.areaId in setOf(1,2,3) }

Because I don't know the number of items that my list will have

1
  • You don't strictly need a set to call in; it would work with in selectedAreasList directly.  But most Set implementations are much more efficient at this (as they can look up the hash code directly instead of scanning through each time), so it's usually a good idea. Commented Nov 21, 2020 at 21:17

1 Answer 1

1

Instead of using setOf on a list, use the existing toSet function defined for all Sequences:

selectedAreasList = listOf(1, 2, 3)
// make a set before the filter to avoid repeatedly making a set
selectedAreasSet = selectedAreasList.toSet()
initialItemProductList.filter { id.areaId in selectedAreasSet }
Sign up to request clarification or add additional context in comments.

2 Comments

like a charm... Thanks dude
I think you've mis spelled collection with sequence, although it is defined for sequences as well :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.