Amitabh Ranjan
Amitabh Ranjan

Reputation: 1500

Find an element in a List by comparing with another list in scala

I have two list.

val lis1= List("pt1","pt2","")

val lis2= List("pt1","")

I need to find the empty string in the lis1 so I am trying to do

val find= lis1.find(lis=>lis2.contains(""))

Here instead returning me "" , its returning me ("pt1"). Kindly help me how can I get empty string instead of "pt1"

Upvotes: 0

Views: 1968

Answers (1)

Andy
Andy

Reputation: 5228

It sounds like you want the intersection of the two lists. You can use filter + contains, similar to your original approach. Alternative you can use the intersect method.

val lis1 = List("pt1", "pt2", "")
val lis2 = List("pt1", "")

lis1.filter(item => lis2.contains(item))
// > res0: List[String] = List(pt1, "")

lis1.intersect(lis2)
// > res1: List[String] = List(pt1, "")

Upvotes: 1

Related Questions