簡體   English   中英

在kotlin中,使用地圖后如何過濾列表

[英]In kotlin, how to filter a list after using the map

如何在地圖之后通過 id 列表過濾列表? 我用 findAll 得到一個對象列表,使用 map 發送一個列表,但我只需要顯示列表中的一些 id,例如:(1,2,5,7)。 我不知道要在過濾器中放什么以使過濾后的地圖以我需要的方式呈現

@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() }
        )))
    }
}

您可以使用contains()檢查值列表中是否存在值。

假設您列表中的對象具有參數id並且您要過濾的那些 id 總是相同的,您可以實現您描述的內容,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM