Reputation: 610
I would like to left join two list, without lost of the data. Example below:
// two lists
val b = List("John","Alice","Gregor","Mike") // base list
val l = List((List(1,2,3),"Mike"), (List(3,1,2), "Alice")) // list to left join
// sorted
val bs = b.sorted // List("Alice","Gregor","John","Mike")
val ls = l.sortBy(_._2) // List((List(3,1,2), "Alice"),(List(1,2,3),"Mike"))
// left join - expected result:
// Alice and Mike found in both lists, Gregor and John appear only in the base list
// List(("Alice",(List(3,1,2),"Alice"), ("Gregor",Nil), ("John",Nil), ("Mike",List("Alice","Gregor","John","Mike"))))
I found some methods, but they seem to be not proper for me:
- zip - simple 'copy/paste' two lists,
- flatMap - remove elements, which doesn't appear in both lists.
Thank you in advance.
Upvotes: 4
Views: 601
Reputation: 2851
You can do it, using a Map
:
val m = l.map(_.swap).toMap
val res = bs.map(elem => elem -> m.get(elem).map(_ -> elem).getOrElse(Nil))
println(res)
// Displays
// List((Alice,(List(3, 1, 2),Alice)), (Gregor,List()), (John,List()), (Mike,(List(1, 2, 3),Mike)))
Upvotes: 1
Reputation: 3638
Using this
bs.map{
str =>
(str, ls.find(_._2 == str).getOrElse(Nil))
}
you will get an output as
List((Alice,(List(3, 1, 2),Alice)), (Gregor,List()), (John,List()), (Mike,(List(1, 2, 3),Mike)))
Upvotes: 1
Reputation: 48430
Try
b
.map(key => key -> l.find(_._2 == key))
.map {case (key, value) => key -> value.getOrElse(Nil) }
which outputs
List((John,List()), (Alice,(List(3, 1, 2),Alice)), (Gregor,List()), (Mike,(List(1, 2, 3),Mike))
Upvotes: 4