1

I have a linked list with object person.

Class person{
   int age;
   String name;
   String address
}

Now, I want to check if object is contained in the list. But my problem, is that only some fields are relevant. For example, I want to check if the person: tony, age 18 is contained in the list.

Are list support it? If not, which data structure support it?

6 Answers 6

3

Well, you could use a simple loop.

for (int i=0; i<list.size(); i++) {
    Person p = list.get(i);
    if (p.name.equals("Tony") && p.age == 18) {
        //p is the person you were looking for
        //do whatever you wanted to do with p
        //if you don't want to include duplicate "Tonies" then add a break;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Well, you can put your persons in a List<Person> and iterate over it:

for(Person p : personList){
    if ("tony".equals(p.name) && p.age == 18){
        //match - do some stuff
        ;
    }
}

Comments

2

List has method contains(obj) it compares (o==null ? e==null : o.equals(e))

So if you override equals method in your Person class, and you could do list.contains(obj) it returns you true/false directly.

1 Comment

The problem with this is that then everywhere you use the Person object they will be considered equal if they have the same name and age and it will disregard the address, so Tony age 18 in San Francisco will be considered equal to Tony age 18 in Chicago, which probably isn't right in all cases.
2

This class can be mapped to a table at databse and you want to execute a select * from person where age > 18.

There are 2 cases to do it: on is without index: the databse will read all values and it will do a compare to age field value, the same is with list: parse the list elements and you will find for sure, but this is the slowest method.

Other one is an optimisation for search: add an index to database to relevant fields. In list you can have other data collection to speed up searching.

For example: LinkedHashSet> indexedByAge just ask it who is 18 or before 18 an you will get a series of ArrayList which need to merge to one with addAll() method.

Comments

2

You can check like this

ArrayList<Person> persons=new ArrayList<Person>();

To check persons

do like this

for(Person p : persons)
{
 if (p.name.equals("Tony") && p.age == 18)
}

Comments

1

A bit messy, but you could override the equals method as follows:

class Person
{
   int age;
   String name;
   String address;

   Person(int a, String n, String ad) { age = a; name = n; address = ad; };
   @Override
   public boolean equals(Object o)
   {
      Person p = (Person)o;
      if (p.age != -1 && age != -1 && p.age != age)
         return false;
      if (p.name != null && name != null && !p.name.equals(name))
         return false;
      if (p.address != null && address != null && !p.address.equals(address))
         return false;
      return true;
   }
}

I'm assuming -1 and null are uninitialized values.

Usage:

ArrayList<Person> arr = new ArrayList<Person>();
arr.add(new Person(18,"tony","here"));
System.out.println(arr.contains(new Person(18,"tony",null))); // prints true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.