'In kotlin, how to filter a list after using the map

How I filter a list by an list of id, after a map? I get an object list with findAll, use map to send a list, but I need show the list only some ids eg: (1,2,5,7). I don't know what to put inside the filter for it to bring the filtered map the way I need it

@Service
class RecommendationService(val reasonRepository: ReasonRepository) {
    @Transactional(readOnly = true)
    fun getLoanAndReasonDecision(loanAmount: Int): ReasonResponse {
        val list = reasonRepository.findAll()
        return if (loanAmount <= LOAN_LOW) {
            ReasonResponse(LOW)
        } else if (loanAmount in LOAN_MEDIUM_FIRST_PARAMETER..LOAN_MEDIUM_SECOND_PARAMETER) {
            ReasonResponse(MEDIUM, (
                list.map { it.toDto()}.filter { it ->  }
            ))
        } else ReasonResponse(HIGH, ((
                list.map { it.toDto() }
        )))
    }
}


Solution 1:[1]

You can use contains() to check if a value is present in a list of values.

Assuming that the objects of your list have the parameter id and that those ids you want to filter are always going to be the same, you can achieve what you describe like following:

list.map { it.toDto()}.filter { listOf(1,2,5,7).contains(it.id)}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paplusc