-3

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

Ex : Employee ( empname, email, mobile)
Employee e = new Employee();
e.setEmpName("chandu");
e.setEmail("chandu@gmail.com");
e.setMobile("9030128664");
arrayList1.add(e);


Employee e1 = new Employee();
e1.setEmpName("ramesh");
e1.setEmail("ramesh@gmail.com");
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)

Farvardin
  • 5,336
  • 5
  • 33
  • 54
user3291914
  • 39
  • 1
  • 9
  • Implement `equals()` and `hashCode()` methods. Then you can iterate the lists and check if there are duplicates – XtremeBaumer May 25 '18 at 13:06
  • Possible duplicate: ["How do I remove repeated elements from ArrayList? "](https://stackoverflow.com/questions/203984/how-do-i-remove-repeated-elements-from-arraylist) – Alex May 25 '18 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. – user3298823 May 25 '18 at 13:09
  • 1
    Could you not just combine both lists into a temporary `LinkedList` or a `Set` which will remove the duplicates? – Alex May 25 '18 at 13:10

2 Answers2

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,

Kovacic
  • 1,473
  • 6
  • 21
  • This is not what is needed. He has **2** lists which can contain the same element and he wants to find those elements – XtremeBaumer May 25 '18 at 13:09
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;
        }
    }
}
Sree
  • 374
  • 2
  • 10