Todd M
Todd M

Reputation: 1010

searching a list for objects with same date

I'm spinning a bit on searching a list using find, find with pattern matching, filter, foldLeft (as well as using Joda for date equality).

I need to search a list of objects and find matches with the same date as a different object.

case class DD(time: Date, value: Double)
case class UT(time: Date, name: String, value: Double)

Given a list of UTs

var sdf = new SimpleDateFormat("yyyy-MM-dd")
var utList = Seq(UT(sdf.parse("2012-02-11"), "adf", 1), UT(sdf.parse("2012-02-13"), "adf", 2),UT(sdf.parse("2012-02-16"), "addf", 3)

Most efficient way to find all UTs with matching DD.time?

Or, even better, split the list into two sublists where one list contains no matches and the other list contains UTs with matching DD.time?

Upvotes: 0

Views: 175

Answers (2)

bjartek
bjartek

Reputation: 949

use utList.filter(ut => ut.time == dd.time) to get all matching time

use utList.partition(ut => ut.time == dd.time) to partition.

Given that dd is an instance of DD.

Upvotes: 2

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340903

I think I am missing something, but...:

val (matchingDate, notMatchingDate) =
  utList partition {_.time == sdf.parse("2012-02-13")}

If you just have a single DD instance someDd:

utList partition {_.time == someDd.time}

Upvotes: 0

Related Questions