I have a list of object PersonInfo, if certain fields in the PersonInfo object are same with another PersonInfo object, i'll say these two objects are duplicated. example:
case class PersonInfo(
firstName: Instant,
lastName: Instant,
ssn: String,
email: String
)
if two PersonInfo objects have same 'ssn', they are duplicated record. my list looks like this:
val list = List(pi1, pi2, pi3)
pi1 is: PersonInfo("foo", "foo", "123-456-789", "[email protected]")
pi2 is: PersonInfo("bar", "bar", "456-123-789", "[email protected]")
pi3 is: PersonInfo("gee", "gee", "123-456-789", "[email protected])
how can i filter the list to only return list of (pi1 and pi3) since pi1 and pi3 are duplicated:
list.filter(f => pi1.ssn == pi3.ssn) => ???
and I expect it to return List(pi1, pi2)
Group them, keep only the duplicates, return as List.
list.groupBy(_.ssn).values.filter(_.length > 1).flatten.toList
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With