I have a list of DTO received from a DB, and they have an ID. I want to ensure that my list contains an object with a specified ID. Apparently creating an object with expect
Well, i think your approach is a bit overcomplicating the problem. You said:
I have a list of DTO received from a DB, and they have an ID.
Then probably you should use a DTO class to hold those items. If so put id getter and setter inside that class:
public class DTO implements HasId{
void setId(Long id);
Long getId();
}
That's enough for iterate through and ArrayList and search for the desired id. Extending ArrayList class only for adding the "compare-id" feautre seems overcomplicated o me. @Nikita Beloglazov make a good example. You can generalize it even more:
public boolean containsId(List list, long id) {
for (HasId object : list) {
if (object.getId() == id) {
return true;
}
}
return false;
}