In Java, we can remove an object from a list by using the method list.remove(Object o), this method uses the equals method of the object to identify the element in the list. But, this requires us to override the equals method(otherwise the default implementation of the equals method would be used, which involves comparing the references). But if we are dealing with a list of objects and the developer doesn’t have the liberty to change the source code of the object to implement the equals method, what can we do? I am wondering why java has not provided a method like list.remove(Object o, Comparator c), because then we could remove the object based on custom equality condition(implemented by the comparator), any solutions to this problem?
Share
If you want anything more complicated, you can always iterate yourself, performing any comparisons you want, and then use
List.remove(int)to remove by index.