Scala difference of two lists

后端 未结 3 1230
广开言路
广开言路 2021-02-01 14:36

I have two lists:

val list1 = List(\"word1\",\"word2\",\"word2\",\"word3\",\"word1\")
val list2 = List(\"word1\",\"word4\")

I want to remove al

相关标签:
3条回答
  • 2021-02-01 14:55

    You can

    val unwanted = list2.toSet
    list1.filterNot(unwanted)
    

    to remove all items in list2. (You don't need knowledge of duplicates in list2.)

    0 讨论(0)
  • 2021-02-01 14:56
    val list1 = List("word1","word2","word2","word3","word1")
    val list2 = List("word1","word4") 
    list1 diff list2
    

    This will do it.

    0 讨论(0)
  • 2021-02-01 14:58

    You could try this:

    val list1 = List("word1","word2","word2","word3","word1")
    val list2 = List("word1","word4")
    
    println(list1.filterNot(list2.contains(_)))
    
    0 讨论(0)
提交回复
热议问题