Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering a scala string sequence based on a list of strings

I have a Scala sequence which is of format ("Apple-fruit", "Banana-fruittoo", "Chocolate-notafruit") and I have another Scala list of format ("Apple", "Banana")

I want to filter my first sequence based on second list so that my final output is ("Apple-fruit", "Banana-fruittoo"). Could anyone help me with this filter function?

like image 462
user461112 Avatar asked Jan 28 '26 06:01

user461112


1 Answers

Seq("Apple-fruit", "Banana-fruittoo", "Chocolate-notafruit")
  .filter(x => Seq("Apple", "Banana").exists(y => x.contains(y)))
// Seq("Apple-fruit", "Banana-fruittoo")

For each item (x) of the seq to filter, we check if at least one element (y) of the filtering seq exists such as x contains y.

like image 145
Xavier Guihot Avatar answered Jan 29 '26 22:01

Xavier Guihot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!