I came across with kotlin equals function to compare two list of same type. It works fine for pure Kotlin with data classes.
I'am using a Java library in Kotlin project in which a callback method returns a list of objects for a time interval of X seconds. Trying to compare the old list with new list for every call, but equals returns false even the items are same and equal.
val mOldList: MutableList<MyObject>()? = null
override fun updatedList(list: MutableList<MyObject>){
// other code
if (mOldList.equals(list)) // false everytime
}
Is this because of Java's equals method from library?
Alternative suggestions for list compare would be appreciative.
If you don't bother about order of elements in both lists, and your goal is to just check that two lists are of exactly same elements, without any others, you can consider two mutual containsAll
calls like:
var list1 = mutableListOf<String>()
var list2 = mutableListOf<String>()
if(list1.containsAll(list2) && list2.containsAll(list1)) {
//both lists are of the same elements
}