Home > Software engineering >  Compare two lists of Scala to check oneOf/Contains
Compare two lists of Scala to check oneOf/Contains

Time:01-16

lets say I have 2 lists:

listOfCountries =       List("United States", "Belgium", "Germany")
SecondlistOfCountries = List("Italy", "France", "Germany")

oneOf:

I want to check if there is at least one item that appears in both lists (the example will be "true")

Contains:

I want to check if one of the items in the SecondlistOfCountries list contains one of the items in the listOfCountries list (as a substring)

how would you do something like this in a clean Scala way?

thanks

CodePudding user response:

oneOf can be also written as:

listOfCountries.exists(SecondlistOfCountries.contains)

For contains I suggest the same as @Tim.

CodePudding user response:

oneOf:

listOfCountries.exists(SecondlistOfCountries.contains)

[ Thanks to @Ava for this version, which is much better than my original solution ]

contains:

SecondlistOfCountries.exists(c => listOfCountries.exists(c.contains))
  •  Tags:  
  • Related