Reputation: 477
i have this code :
case class DataText(name:String)
val dataModels = Seq(DataText("a.dm"),DataText("b.dm"),DataText("c.dm"),DataText("d.dm"),DataText("e.dm"),DataText("f.dm"))
val dataReports = Seq(DataText("a.d0"),DataText("b1.do"),DataText("c2.do"),DataText("d.do"),DataText("e3.do"),DataText("f5.do"))
how can i match dataModels and dataReports, when an item in dataModels split by "." like name.split(".").head can match to dataReports split by "." like name.split(".").head
The result could be:
Seq(DataText("a.dm"),DataText("d.dm"))
i have tried with map and an embedded filter but don't work.
Upvotes: 0
Views: 38
Reputation: 22439
I would transform dataReports
into a Set
of the target sub-elements for filtering via contains
(which is a constant time O(1) operation):
val dataReportsSet = dataReports.map(_.name.split("\\.")(0)).toSet
dataModels.filter(dm => dataReportsSet.contains(dm.name.split("\\.")(0)))
// res1: Seq[DataText] = List(DataText(a.dm), DataText(d.dm))
Upvotes: 1