如何比较Kotlin中的两个数组?

时间:2022-03-04 08:07:07

Given some arrays in Kotlin

给出了Kotlin中的一些数组

let a = arrayOf("first", "second")
val b = arrayOf("first", "second")
val c = arrayOf("1st", "2nd")

Are there built-in functions to the Kotlin std-lib that tests two arrays for (value) equality for each element?

Kotlin std-lib是否有内置函数可以为每个元素测试两个数组的(值)相等性?

Thus resulting in:

从而导致:

a.equals(b) // true
a.equals(c) // false

equals() is actually returning false in both cases, but maybe there are built-in functions to Kotlin that one could use?

在这两种情况下,equals()实际上都返回false,但是Kotlin可能会使用内置函数吗?

There is the static function java.utils.Arrays.deepEquals(a.toTypedArray(), b.toTypedArray()) but I would rather prefer an instance method as it would work better with optionals.

有静态函数java.utils.Arrays.deepEquals(a.toTypedArray(),b.toTypedArray())但我更喜欢实例方法,因为它可以更好地与optionals一起使用。

4 个解决方案

#1


18  

In Kotlin 1.1 you can use contentEquals and contentDeepEquals to compare two arrays for structural equality. e.g.:

在Kotlin 1.1中,您可以使用contentEquals和contentDeepEquals来比较两个数组的结构相等性。例如。:

a contentEquals b // true
b contentEquals c // false

In Kotlin 1.0 there are no "built-in functions to the Kotlin std-lib that tests two arrays for (value) equality for each element."

在Kotlin 1.0中,没有“Kotlin std-lib的内置函数,它为每个元素测试两个数组的(值)相等性。”

"Arrays are always compared using equals(), as all other objects" (Feedback Request: Limitations on Data Classes | Kotlin Blog).

“总是使用equals()比较数组,就像所有其他对象一样”(反馈请求:对数据类的限制| Kotlin博客)。

So a.equals(b) will only return true if a and b reference the same array.

因此,如果a和b引用相同的数组,则a.equals(b)将仅返回true。

You can, however, create your own "optionals"-friendly methods using extension functions. e.g.:

但是,您可以使用扩展功能创建自己的“选项”友好方法。例如。:

fun Array<*>.equalsArray(other: Array<*>) = Arrays.equals(this, other)
fun Array<*>.deepEqualsArray(other: Array<*>) = Arrays.deepEquals(this, other)

P.S. The comments on Feedback Request: Limitations on Data Classes | Kotlin Blog are worth a read as well, specifically comment 39364.

附:对反馈请求的评论:对数据类的限制| Kotlin博客也值得一读,特别是评论39364。

#2


19  

Kotlin 1.1 introduced extensions for comparing arrays by content: contentEquals and contentDeepEquals.

Kotlin 1.1引入了按内容比较数组的扩展:contentEquals和contentDeepEquals。

These extensions are infix, so you can use them the following way:

这些扩展名是中缀,因此您可以通过以下方式使用它们:

val areEqual = arr1 contentEquals arr2

#3


4  

And if you want to compare contents of two Collections ignoring the order you can add this simple extension:

如果你想比较两个集合的内容忽略顺序,你可以添加这个简单的扩展:

infix fun <T> Collection<T>.sameContentWith(collection: Collection<T>?)
    = collection?.let { this.size == it.size && this.containsAll(it) }

...and use it like this:

......并像这样使用它:

a = mutableListOf<String>()
b = mutableListOf<String>()

isListsHasSameContent = a sameContentWith b

#4


2  

For a simple equals (not deep equals!):

对于一个简单的等号(不等于!):

otherArray.size == array.size && otherArray.filter { !array.contains(it) }.isEmpty()

This code will compare the size and the items. The items are compared with .equals().

此代码将比较大小和项目。这些项目与.equals()进行比较。

#1


18  

In Kotlin 1.1 you can use contentEquals and contentDeepEquals to compare two arrays for structural equality. e.g.:

在Kotlin 1.1中,您可以使用contentEquals和contentDeepEquals来比较两个数组的结构相等性。例如。:

a contentEquals b // true
b contentEquals c // false

In Kotlin 1.0 there are no "built-in functions to the Kotlin std-lib that tests two arrays for (value) equality for each element."

在Kotlin 1.0中,没有“Kotlin std-lib的内置函数,它为每个元素测试两个数组的(值)相等性。”

"Arrays are always compared using equals(), as all other objects" (Feedback Request: Limitations on Data Classes | Kotlin Blog).

“总是使用equals()比较数组,就像所有其他对象一样”(反馈请求:对数据类的限制| Kotlin博客)。

So a.equals(b) will only return true if a and b reference the same array.

因此,如果a和b引用相同的数组,则a.equals(b)将仅返回true。

You can, however, create your own "optionals"-friendly methods using extension functions. e.g.:

但是,您可以使用扩展功能创建自己的“选项”友好方法。例如。:

fun Array<*>.equalsArray(other: Array<*>) = Arrays.equals(this, other)
fun Array<*>.deepEqualsArray(other: Array<*>) = Arrays.deepEquals(this, other)

P.S. The comments on Feedback Request: Limitations on Data Classes | Kotlin Blog are worth a read as well, specifically comment 39364.

附:对反馈请求的评论:对数据类的限制| Kotlin博客也值得一读,特别是评论39364。

#2


19  

Kotlin 1.1 introduced extensions for comparing arrays by content: contentEquals and contentDeepEquals.

Kotlin 1.1引入了按内容比较数组的扩展:contentEquals和contentDeepEquals。

These extensions are infix, so you can use them the following way:

这些扩展名是中缀,因此您可以通过以下方式使用它们:

val areEqual = arr1 contentEquals arr2

#3


4  

And if you want to compare contents of two Collections ignoring the order you can add this simple extension:

如果你想比较两个集合的内容忽略顺序,你可以添加这个简单的扩展:

infix fun <T> Collection<T>.sameContentWith(collection: Collection<T>?)
    = collection?.let { this.size == it.size && this.containsAll(it) }

...and use it like this:

......并像这样使用它:

a = mutableListOf<String>()
b = mutableListOf<String>()

isListsHasSameContent = a sameContentWith b

#4


2  

For a simple equals (not deep equals!):

对于一个简单的等号(不等于!):

otherArray.size == array.size && otherArray.filter { !array.contains(it) }.isEmpty()

This code will compare the size and the items. The items are compared with .equals().

此代码将比较大小和项目。这些项目与.equals()进行比较。