Last Updated: February 06, 2018
·
141
· prayagupd

filter if some property value is in list of defined data

Given data and filterList,

scala> case class Customer(name: String, preference: Option[String])
defined class Customer

scala> Seq(Customer("prayagupd", Some("music")), Customer("stevenwilson", Some("soccer")))
res3: Seq[Customer] = List(Customer(prayagupd,Some(music)), Customer(stevenwilson,Some(soccer)))

scala> val filterPrefs = Seq("music", "reading")
filterPrefs: Seq[String] = List(music, reading)

Apply the filter,

scala> res3.filter(c => filterPrefs.exists(p => c.preference.contains(p)))
res16: Seq[Customer] = List(Customer(prayagupd,Some(music)))