Finding out if a list of Objects contains something with a specified field value?
Solution 1:
I propose to create simple static method like you wrote, without any additional interfaces:
public static boolean containsId(List<DTO> list, long id) {
for (DTO object : list) {
if (object.getId() == id) {
return true;
}
}
return false;
}
Solution 2:
I suggest you just override the equals
in your SearchableDto
it would be something like:
public boolean equals(Object o){
if (o instanceof SearchableDto){
SearchableDto temp = (SearchableDto)o;
if (this.id.equals(temp.getId()))
return true;
}
return false;
}
In this case contains
should work probably if it has the same id
;