Home > Back-end >  How to check if a list of custom types, contains a subset of values of that type in Kotlin?
How to check if a list of custom types, contains a subset of values of that type in Kotlin?

Time:12-22

I have a list of custom types in Kotlin:

val dates = mutableListOf(
    Date(year = 2020, month = 4, day = 3),
    Date(year = 2021, month = 5, day = 16),
    Date(year = 2020, month = 1, day = 29)
)

And a set of "months" I need to ensure are in the list:

val months = listOf(1, 4)

Is ther a way to check if dates contains months ? I feel like there could be a way to do this using the .containsAll function on the initial list, but am not sure?

CodePudding user response:

Does this solve your problem?

dates.map { it.month }.containsAll(months)

CodePudding user response:

Are you talking about something like this that gives you only the days that have month 1 or 4??

val months = dates.filter { it.month == 1 || it.month == 4 }
  • Related