1

In my java code I have a structure Person:

public class Person {
  String name;
  String distance;
  String address;
  String field1;
  String field2;
}

Now, I have an ArrayList<Person> people that contains couple objects. I also have another ArrayList<Person> otherPeople that contains other objects.

I want to produce a 3rd list that contains all objects from people that are not already in otherPeople.

But I need to compare the objects only by their name, distance and address, I don't care of values of field1 and field2.

I thought about creating 2 for loops:

for (Person newPerson: people) {
   for (Person oldPerson: otherPeople) {
       if(newPerson.getName().equals(oldPerson.getName()) &&
         newPerson.getDistance().equals(oldPerson.getDistance()) &&
         newPerson.getAddress().equals(oldPerson.getAddress()) {

but I don't know how to proceed, especially since I cannot remove elements from the list I'm iterating through... Can you help me with that?

3
  • As you want to create a third List you don't have to remove anything from the other two. Just put the elements that match your if condition into that third list (which you should create before the for loop, of course). Commented Mar 12, 2017 at 15:29
  • yes, but then the 3rd list will contain elements that are in both lists, and I want to have a list only with new people that are not in the 2nd list Commented Mar 12, 2017 at 15:30
  • Got me. Will write a fully qualified answer. Commented Mar 12, 2017 at 15:32

3 Answers 3

5

Could you override equal method for Person class? Then you will be able to delete person from collection using methods remove or removeAll.

class Person {
        String name;
        String distance;
        String address;
        String field1;
        String field2;

        @Override
        public boolean equals(Object o) {
            if (this == o)
                return true;
            if (o == null || getClass() != o.getClass())
                return false;
            Person person = (Person) o;
            return Objects.equals(name, person.name) &&
                    Objects.equals(distance, person.distance) &&
                    Objects.equals(address, person.address);
        }

        @Override
        public int hashCode() {
            return Objects.hash(name, distance, address);
        }
    }

    class Example {
       public static void main(String[] args) {
            Person person1 = new Person();
            person1.address = "address_1";
            person1.distance = "distance_1";
            person1.name = "name_1";
            person1.field1 = "field1_1";
            person1.field2 = "field2_2";

            Person person2 = new Person();
            person2.address = "address_2";
            person2.distance = "distance_2";
            person2.name = "name_2";
            person2.field1 = "field1_2";
            person2.field2 = "field2_2";

            ArrayList<Person> people = new ArrayList<>(Arrays.asList(person1, person2));
            System.out.println(people);
            ArrayList<Person> otherPeople = new ArrayList<>(Arrays.asList(person1));
            people.removeAll(otherPeople);
            System.out.println(people);
        }
    }
Sign up to request clarification or add additional context in comments.

5 Comments

I can, but I don't know how should I proceed, could you show me some simple code snippet?
+1 for Objects.hash(Object...) // However, please consider OP wants to exclude the members field1 and field2.
if you don't want to overide hashcode and equal check my answer ;)
This answer is functionally correct but ignores the fact that the OP wanted to compare WITHOUT field1 and field2, therefore both equals() and hashCode() should not include those members.
Sorry, my fault.
1

You could try something like :

public static void main(String[] args) {

    ArrayList<Person> people = new ArrayList<>();
    ArrayList<Person> otherPeople = new ArrayList<>();

    ArrayList<Person> peopleDistinct = new ArrayList<>(people);

    peopleDistinct.removeAll(otherPeople);

}

But first you have to re-define equals method for Person class.

Edit : Here an example of how you could override equals method:

@Override 
public boolean equals(Object other) {

    boolean result = false;

    if (other instanceof Person) {

        Person that = (Person) other;
        result = (this.name == that.name && this.distance == that.distance && this.address == other.address);
    }

    return result;
}

Note : that you should add field1 and field2 if they are necessary for the the equals function.

1 Comment

Thanks @Mouad, that looks really promising, could you also show me some code snipped about the equals method? I don't know how to use it with only 3 fields that I want to consider while comparing objects...
0
  1. Implement equals() and hashcode() method in your Person class, and calculate the hashcode as a function of name, distance, address. For more detail you can search how to write a good hash function. Remember that in your equals method you write a logic that treats two objects as equal if two both have the same name, distance and address values. However don't forget to implement hashcod() also, as not implementing it might have further complications when you use hashset and hashmap.

  2. Then compare each person objcts using a for loop the way you already started.

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.