-3

I have two lists (arrayList1, arrayList2) contain duplicate objects.

Ex : Employee ( empname, email, mobile)
Employee e = new Employee();
e.setEmpName("chandu");
e.setEmail("[email protected]");
e.setMobile("9030128664");
arrayList1.add(e);


Employee e1 = new Employee();
e1.setEmpName("ramesh");
e1.setEmail("[email protected]");
e1.setMobile("9154618845");
arrayList2.add(e);
arrayList2.add(e1);

In the above lists arrayList1 and arrayList2 contain one same object with same value. I have compared arrayList1 and arrayList2 and if they contains any duplicate elements I want to remove that duplicate element.

Can anyone suggest me how to compate two objects and remove duplicate objects

Note : I want to compare two lists with all the values (empname, email, mobile)

4
  • Implement equals() and hashCode() methods. Then you can iterate the lists and check if there are duplicates Commented May 25, 2018 at 13:06
  • Possible duplicate: "How do I remove repeated elements from ArrayList? " Commented May 25, 2018 at 13:07
  • Another way is to use a map as a supporting object so you don't have to do a lot of iterations if you're lists grow large. Commented May 25, 2018 at 13:09
  • 1
    Could you not just combine both lists into a temporary LinkedList or a Set which will remove the duplicates? Commented May 25, 2018 at 13:10

2 Answers 2

3

There are several approaches, here are few that I can think of:

1. plain Java

public void removeDuplicatesFromList() {
    List<Integer> listWithDuplicates = Lists.newArrayList(0, 1, 2, 3, 0, 0);
    List<Integer> listWithoutDuplicates = new ArrayList<>(new HashSet<>(listWithDuplicates));

    assertThat(listWithoutDuplicates, hasSize(4));
}

2. with java (lambda)

public void removeDuplicatesFromList() {
    List<Integer> listWithDuplicates = Lists.newArrayList(1, 1, 2, 2, 3, 3);
    List<Integer> listWithoutDuplicates = listWithDuplicates.stream()
     .distinct()
     .collect(Collectors.toList());
}

3. with Guava

public void removeDuplicatesFromList() {
    List<Integer> listWithDuplicates = Lists.newArrayList(0, 1, 2, 3, 0, 0);
    List<Integer> listWithoutDuplicates = Lists.newArrayList(Sets.newHashSet(listWithDuplicates));

    assertThat(listWithoutDuplicates, hasSize(4));
}

Hope this helps,

Sign up to request clarification or add additional context in comments.

1 Comment

This is not what is needed. He has 2 lists which can contain the same element and he wants to find those elements
0

override equals method in your employee class, then you can compare two employee objects easily.. Employee.java

public class Employee {
    private String name;
    private String email;
    private String mobile;
    //setters and getters
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Employee) {
            Employee objUser = (Employee) obj;
            if(this.name.equals(objUser.getName())&&this.email.equals(objUser.getEmail())&&this.mobile.equals(objUser.getMobile()))
                return true;
        } else {
            return false;
        }
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.