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.
Use a
for
loop to iterate through the list. In the body of the loop, check if the telephone number of the person in the list is the telephone number you're looking for. If yes, then return the person.See The
for
Statement in Oracle's Java Tutorials.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.First of all, why are you not having a
List<Integer>
for storing all yourtelephoneNumbers
for a particular person. That way, you won't have to create a separatePerson
instance for eachtelephoneNumber
for the samepersonId
, which simply makes no sense.You can change your attributes of Person class to: -
And then have a list of Person, as you are having.
To find a
Person
with a particular telephoneNumber, you need to iterate through yourList<Person>
.For implementing
hashCode
andequals
you can observe this tutorial.You need to iterate the array, check the persons one by one for phone number, and when you get to the one you need just assign it to a variable.
Many solutions, define two persons as equal by their phone number but what if two persons that live in the same house and / or have the same phone number are added to the list? Which one is the correct one?.
Before rushing to find the person, you have to define a way to determine if two persons are, indeed, equal without ambiguous results. Unless you restrict the creation of persons based on that very phone number by making it unique (you do not clarify this in your question, so I assume there is no such restriction), the result of the search is undefined.
You're using an
ArrayList
so you can't guarantee even a result by insertion order.I suggest you base the equality test in a person's
ID
instead of its phone. To prevent the modification ofid
's just define a getter for it and do not define asetId
method at all. Then, you can redefineequals
(andhashcode
if you feel like it) based onid
.