Compare two Lists for equality in Kotlin
This article explores different ways to compare two lists for equality in Kotlin.
1. Using == operator
A simple solution to check two lists for equality is using the == operator. It returns true if the size of both lists is the same, and all corresponding pairs of elements in both lists are equal.
|
1 2 3 4 5 6 7 8 9 10 11 |
fun main() { val first = listOf(1, 2, 3, 4, 5) val second = listOf(1, 2, 3, 4, 5) val isEqual = first == second // null-safe if (isEqual) { println("Both lists are equal") } else { println("Both lists are not equal") } } |
Output:
Both lists are equal
This is equivalent to calling the equals() function. Alternatively, you can use the deepEquals() function for comparing two lists in Kotlin. It returns true if the lists are “deeply” equal to each other and false otherwise.
2. Using zip() function
Another plausible way is to use the zip() function that returns the list of pairs built from the elements of both lists with the same index. Since the returned list has the length of the shortest list, you should compare the size of both lists first.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
fun isEqual(first: List<Int>, second: List<Int>): Boolean { if (first.size != second.size) { return false } return first.zip(second).all { (a, b) -> a == b } } fun main() { val first = listOf(1, 2, 3, 4, 5) val second = listOf(1, 2, 3, 4, 5) val isEqual = isEqual(first, second) if (isEqual) { println("Both lists are equal") } else { println("Both lists are not equal") } } |
Output:
Both lists are equal
3. Comparing List of objects
To compare two lists of objects in Kotlin, the objects are required to have a correct and stable implementation of the equals and hashCode functions. The following program demonstrates its usage:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
class Person(private var name: String, private var age: Int) { override fun equals(other: Any?): Boolean { if (this === other) return true if (javaClass != other?.javaClass) return false other as Person if (name != other.name) return false if (age != other.age) return false return true } override fun hashCode(): Int { var result = name.hashCode() result = 31 * result + age return result } } fun main() { val p1 = listOf(Person("James", 18), Person("Mary", 20), Person("Jennifer", 25)) val p2 = listOf(Person("James", 18), Person("Mary", 20), Person("Jennifer", 25)) val isEqual = p1 == p2 if (isEqual) { println("Both lists are equal") } else { println("Both lists are not equal") } } |
Output:
Both lists are equal
Alternatively, you can use a data class that automatically overrides both the equals() and hashCode() functions.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
data class Person(private var name: String, private var age: Int) fun main() { val p1 = listOf(Person("James", 18), Person("Mary", 20), Person("Jennifer", 25)) val p2 = listOf(Person("James", 18), Person("Mary", 20), Person("Jennifer", 25)) val isEqual = p1 == p2 if (isEqual) { println("Both lists are equal") } else { println("Both lists are not equal") } } |
Output:
Both lists are equal
That’s all about comparing two lists for equality in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)