How to compare two arrays in Kotlin?

前端 未结 5 1248
生来不讨喜
生来不讨喜 2020-12-08 18:59

Given some arrays in Kotlin

let a = arrayOf(\"first\", \"second\")
val b = arrayOf(\"first\", \"second\")
val c =          


        
相关标签:
5条回答
  • 2020-12-08 19:06

    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
    
    0 讨论(0)
  • 2020-12-08 19:09

    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().

    0 讨论(0)
  • 2020-12-08 19:12

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

    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."

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

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

    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.

    0 讨论(0)
  • 2020-12-08 19:12

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

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

    val areEqual = arr1 contentEquals arr2
    
    0 讨论(0)
  • 2020-12-08 19:24

    In koltin if your array or ArrayList is type of a data Class you can simply compare array :a and array :b

    like this

    if(a == b)

    it will return simple boolean if it matched all the value of both arrays, but if you are matching other-than data data Class then you can use this extension to compare it with single value

    fun <T> Array<T>.isEqual(comparable: Array<T>): Boolean {
        var isChanged = true
    
        if (this.size == comparable.size) {
            for (index in 0 until comparable.size) {
                if (this[index] != comparable[index]) {
                    isChanged = false
                    break
                }
            }
        } else {
            isChanged = false
        }
    
        return isChanged
    }
    

    then you can use your array

        val a = arrayOf("first", "second")
        val b = arrayOf("first", "second")
    
        println(a.isEqual(b)) //true
    
        a[0] = "third"
    
        println(a.isEqual(b)) //false
    
    0 讨论(0)
提交回复
热议问题