Finding an item that matches predicate in Scala

前端 未结 4 1292
失恋的感觉
失恋的感觉 2021-02-01 00:45

I\'m trying to search a scala collection for an item in a list that matches some predicate. I don\'t necessarily need the return value, just testing if the list contains it.

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-01 01:24

    Filter and exists keywords to get the matching values from Lists

    val values = List(1,2,3,4,5,6,7,8,9,10,....,1000) //List -1
    val factors= List(5,7) // List -2
    
    //To get the factors of List-2 from List-1
    values .filter(a => factors.exists(b => a%b == 0)) //Apply different logic for our convenience
    

    Given code helps to get the matching values from 2 different lists

提交回复
热议问题