使用`==`运算符的Array类型的奇怪行为

时间:2022-06-15 21:37:03
scala> List(1,2,3) == List(1,2,3)

res2: Boolean = true

scala> Map(1 -> "Olle") == Map(1 -> "Olle")

res3: Boolean = true

But when trying to do the same with Array, it does not work the same. Why?

但是当尝试对Array执行相同操作时,它的工作方式不同。为什么?

scala> Array('a','b') == Array('a','b')

res4: Boolean = false

I have used 2.8.0.RC7 and 2.8.0.Beta1-prerelease.

我使用过2.8.0.RC7和2.8.0.Beta1-prerelease。

2 个解决方案

#1


18  

Because the definition of "equals" for Arrays is that they refer to the same array.

因为Arrays的“等于”的定义是它们引用相同的数组。

This is consistent with Java's array equality, using Object.Equals, so it compares references.

这与使用Object.Equals的Java的数组相等一致,因此它比较了引用。

If you want to check pairwise elements, then use sameElements

如果要检查成对元素,请使用sameElements

Array('a','b').sameElements(Array('a','b'))

or deepEquals, which has been deprecated in 2.8, so instead use:

或deepEquals,已在2.8中弃用,因此改为使用:

Array('a','b').deep.equals(Array('a','b').deep)

There's a good Nabble discussion on array equality.

关于数组相等性的讨论很好。

#2


0  

The root cause the it is that fact that Scala use the same Array implementation as Java, and that's the only collection that not support == as equality operator.

根本原因是Scala使用与Java相同的Array实现,这是唯一不支持==作为相等运算符的集合。

Also, it's important to note that the chosen answer suggest equally sameElements and deep comparison when actually it's preferred to use:

此外,重要的是要注意所选择的答案建议同样相同的元素和深度比较实际上它是首选使用:

Array('a','b').deep.equals(Array('a','b').deep)

Or, because now we can use == back again:

或者,因为现在我们可以再次使用==

Array('a','b').deep == Array('a','b').deep

Instead of:

代替:

Array('a','b').sameElements(Array('a','b'))

Because sameElements won't for for nested array, it's not recursive. And deep comparison will.

因为sameElements不适用于嵌套数组,所以它不是递归的。而深刻的比较会。

#1


18  

Because the definition of "equals" for Arrays is that they refer to the same array.

因为Arrays的“等于”的定义是它们引用相同的数组。

This is consistent with Java's array equality, using Object.Equals, so it compares references.

这与使用Object.Equals的Java的数组相等一致,因此它比较了引用。

If you want to check pairwise elements, then use sameElements

如果要检查成对元素,请使用sameElements

Array('a','b').sameElements(Array('a','b'))

or deepEquals, which has been deprecated in 2.8, so instead use:

或deepEquals,已在2.8中弃用,因此改为使用:

Array('a','b').deep.equals(Array('a','b').deep)

There's a good Nabble discussion on array equality.

关于数组相等性的讨论很好。

#2


0  

The root cause the it is that fact that Scala use the same Array implementation as Java, and that's the only collection that not support == as equality operator.

根本原因是Scala使用与Java相同的Array实现,这是唯一不支持==作为相等运算符的集合。

Also, it's important to note that the chosen answer suggest equally sameElements and deep comparison when actually it's preferred to use:

此外,重要的是要注意所选择的答案建议同样相同的元素和深度比较实际上它是首选使用:

Array('a','b').deep.equals(Array('a','b').deep)

Or, because now we can use == back again:

或者,因为现在我们可以再次使用==

Array('a','b').deep == Array('a','b').deep

Instead of:

代替:

Array('a','b').sameElements(Array('a','b'))

Because sameElements won't for for nested array, it's not recursive. And deep comparison will.

因为sameElements不适用于嵌套数组,所以它不是递归的。而深刻的比较会。