I have a couple of questions actually.
I have a class Dog with the following instance fields:
private int id;
private int id_mother;
I solved this using java 8 lambdas
int dogId = 2;
return dogList.stream().filter(dog-> dogId == dog.getId()).collect(Collectors.toList()).get(0);
Assuming that you've written an equals method for Dog correctly that compares based on the id of the Dog the easiest and simplest way to return an item in the list is as follows.
if (dogList.contains(dog)) {
return dogList.get(dogList.indexOf(dog));
}
That's less performance intensive that other approaches here. You don't need a loop at all in this case. Hope this helps.
P.S You can use Apache Commons Lang to write a simple equals method for Dog as follows:
@Override
public boolean equals(Object obj) {
EqualsBuilder builder = new EqualsBuilder().append(this.getId(), obj.getId());
return builder.isEquals();
}