Why Kotlin list removeAll doesnt not work in this example:
orderList.addAll(allProducts)
orderList.removeAll(allProducts)
The code above will add the products but not remove them. orderList is a mutableList
Product:
class Product : EmbeddedRealmObject {
var name: String = ""
var category: String = ""
var productDescription: String? = ""
var price: Float = 0F
var imagine: String? = null
}
It's not working because you don't have proper
equals()andhashcode()functions defined for your class Product.removeAllworks by finding elements in your collection where anequals()equality check returns true with elements in the collection you pass to it. It also uses a Set under the hood to optimize this process, sohashcode()must be appropriately defined in relation toequals().You can generate
equals()and matchinghashcode()functions using the options in the IDE, or you can define this as adataclass instead of a plain class.dataclasses automatically have properequals()andhashcode()that use all the properties that are defined in the primary constructor.