You should not use equals like that. Overriding equals
is somewhat complex, you have to verride hashcode
too, for example. A list-based solution with minimal changes:
Class Person{
String name;
int age;
//...
public String getName() {
return name;
}
}
main {
List<Person> list = personDao.findAll();
Person foundPerson = null;
for(Person person : list) {
if (person.getName().equals("joe")) {
foundPerson = person;
break;
}
}
if (foundPerson != null) {
// do something
}
}
However, I would consider using a Map<String, Person>
, with name as key. Overriding equals
in Person while ignoring age does not sound very good, really.