Say I have an object which has an id: String and name: String field.
If I have an array of these objects, if I use the remove method, since technically the Object pointer is different, the object I want won't get removed.
Ex:
List<ObjectA> newList = new ArrayList<>();
// Objecta1(id: a, name: a)
// Objecta2(id: b, name: b)
// Objecta3(id: b, name: b)
newList.add(objecta1);
newList.add(objecta2);
newList.remove(objecta3); --> will equal false
What is the cleanest way to have remove do this correctly?
I searched around and found that I could use removeIf and do a comparison there. I also could use an Iterator and then compare the fields myself. Just wondering if there is a canonical, clean way of doing it in Java. Thanks.
ObjectAoverrideequals()?