user468587
user468587

Reputation: 5031

scala filter list of object if some fields in the object are same

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)

Upvotes: 3

Views: 1047

Answers (2)

Krzysztof Atłasik
Krzysztof Atłasik

Reputation: 22635

I would use groupBy + flatMap:

val pi1 = PersonInfo("foo", "foo", "123-456-789", "[email protected]")
val pi2 = PersonInfo("bar", "bar", "456-123-789", "[email protected]")
val pi3 = PersonInfo("gee", "gee", "123-456-789", "[email protected]")

val list = List(pi1, pi2, pi3)

val onlyDuplicates = list
    .groupBy(_.ssn)
    .flatMap{
      case (_, v) if v.length > 1 => v //take only elements that have 1+ occurence
      case _ => Nil
    }

println(onlyDuplicates) // List(pi1, pi3)

Upvotes: 2

jwvh
jwvh

Reputation: 51271

Group them, keep only the duplicates, return as List.

list.groupBy(_.ssn).values.filter(_.length > 1).flatten.toList

Upvotes: 3

Related Questions