I have a class called Person. It has the following attributes; It has 2 attributes, ID, and Telephone. 1 person can have many telephones, so you may see people with multiple ID’s below.
public ArrayList<Person> all(){
p = new ArrayList<Person>();
p.add(new Person(1,266763));
p.add(new Person(1, 358643));
p.add(new Person(2, 4667763));
return p;
}
There’s another class called PersonDB. and it will have a method called, findPersonWithTheTelephoneNumber(int telephone).
public void findPersonWithTheTelephoneNumber(int telephone) {
Person pp = new Person();
ArrayList<Person> personList = pp.all();
// Now i want to find the Person object that will match the telephone number of these list of personList.
}
The personList, has 3-4 Person objects. I need to search the PersonArrayList and find the object that will match the Person object. How can i get this done ?
Note: i tried personList.contains(). But this doesn’t work.
Make sure you override
Object.equals()andObject.hashCode()for Person class. But you have to have equality check on telephone number assuming telephone number unique. This would not be a solution but a workaround. Use bellum’s answer. Mark it as correct answer.