Is it possible to override Object#equals(Object)
locally when using list.contains(someObject)
?
Example:
class SomeObject {
...
private int id;
private String name;
@Overrdide
public boolean equals(Object other){
...
return this.id == other.id;
}
}
But what if I want another kind of equals when I use list.contains(someObject)
? For example I want to know if a list contains a certain name? Is it possible to override Object#equals(Object)
'anonymously'?
More specific explanation why I would need it:
int objectId = ... // Some event which passes me the attribute of an object but not the object itself
Now I have List<SomeObject> someObjects
and I would like to know if this list contains an object with objectId
without necessarily iterating over it.
One "solution" I could think of would be using Map<Integer, SomeObject> mapping
and then someObject = mapping.get(objectId)
EDIT: My question is not a duplicate since I am specifically asking to override Object#equals(Object)
.