To check if two sets are equal in Kotlin, you can use the ==
operator or the equals()
method.
In this example,
set1
and set2
.==
operator between set1
and set2
to check if they are equal.==
operator returns true
if both sets have the same elements, in the same order, and false
otherwise.fun main() {
val set1 = setOf(1, 2, 3, 4, 5)
val set2 = setOf(1, 2, 3, 4, 5)
println(set1 == set2)
}
true
In this example,
set1
and set2
.equals()
method on set1
and pass set2
as an argument.equals()
method returns true
if both sets have the same elements, in the same order, and false
otherwise.fun main() {
val set1 = setOf(1, 2, 3, 4, 5)
val set2 = setOf(1, 2, 3, 4, 5)
println(set1.equals(set2))
}
true
In this tutorial, we learned How to check if Two Sets are Equal in Kotlin language with well detailed examples.