Search code examples
scala

how to get a set with un-containable values in scala


I am new to scala, and here is the question, below is my set

val set1 = Set("hello world, this is scala new", "hello world, this is scala", "hello world, this is", "hello apple")

and I want to have results like below shows

val set2 = Set("hello world, this is scala new", "hello apple")

since "hello world, this is scala", "hello world, this is" all contained in "hello world, this is scala new", so I would like to remove them in set2, how to achieve this in scala?


Solution

  • val set2 = set1.filterNot(s1 => set1.exists(s2 => s2.contains(s1) && s1 != s2))