Is there an API method that compares contents of a Seq irrespective of order?

前端 未结 2 1179
自闭症患者
自闭症患者 2020-12-03 06:55

Assuming:

val l1 = List(1,2,3) 
val l2 = List(2,3,1)

I want a method that confirms that l1 is equal to l2 (as in same contents but differen

2条回答
  •  没有蜡笔的小新
    2020-12-03 07:45

    If what you want is "these lists contain the same elements, irrespective of order or repetitions":

    l1.toSet == l2.toSet

    If what you want is "these lists contain the same elements, and with the same number of repetitions of each":

    l1.sorted == l2.sorted

    If what you want is "these lists contain the same elements and are the same size, but the number of repetitions of a given element can differ between the two lists":

    l1.size == l2.size && l1.toSet == l2.toSet

提交回复
热议问题