0
public class Employee implements Comparable<Employee> {

    private int id;
    private String name;
    private String salary;
    private String recordStatus;
    private int key;

    public Employee(int id, String name, String salary, int key) {
        super();
        this.id = id;
        this.name = name;
        this.salary = salary;
        this.key = key;
    }
}

Now I have a list of type Employee.

List<Employee> list = new ArrayList<Employee>();
list.add(new Employee(123, "zMadhu", "1000$",1));
list.add(new Employee(332, "bSudhan", "2000$",2));
list.add(new Employee(54, "cKongarass", "3000$",3));
list.add(new Employee(54, "xKongarass", "3000$",4));
list.add(new Employee(54, "aKongarass", "3000$",5));

Now I want to remove data from this list and have only unique IDS. I.E. I am expecting 54,123,332 in another list of type Employee.

Want to see how I can do it. Much appreciate your help here.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
madhu kongara
  • 39
  • 1
  • 4
  • 5
    First, you need to try yourself; when you're stuck with a specific problem, you're welcome to ask about that. – daniu Sep 12 '18 at 13:34
  • 1
    I little tip: try to look at `Set` – Michel_T. Sep 12 '18 at 13:39
  • You need to implement your custom comparator/equal in the class. Then You can put yor List in a set. Search for a tutorial on how to implement a comparator. – Ale Sep 12 '18 at 13:39

4 Answers4

5

First override equals(..) and hashCode() where you use just the id :

...

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof Employee)) return false;

    Employee employee = (Employee) o;

    return id == employee.id;
}

@Override
public int hashCode() {
    return id;
}
...

Second Just create a Set<Employee> which will not accept duplicates Objects like so :

Set<Employee> result = new HashSet<>(list);// [54, 123, 332]

Take a look at a simple Ideone demo

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
2

First of all, this code won't compile because you don't have the comparable interface implemented. So, I just took that out for now assuming you left it out for brevity :).

Assuming you had that... the most sensible thing would be to use a map in the first place.

Assuming you want to start with this list though, you can convert this to a map and log/remove duplicates with a stream:

Map<Integer, Employee> employees = list.stream()
        .collect(Collectors.toMap(k -> k.id, v -> v, (a, b) -> {
    System.out.println("Duplicate found! " + a.id + " taking first one.");
    return a;
}));
System.out.println(employees);

Results:

Duplicate found! 54 taking first one.

Duplicate found! 54 taking first one.

{54=Employee{id=54, name='cKongarass', salary='3000$', recordStatus='null', key=3}, 123=Employee{id=123, name='zMadhu', salary='1000$', recordStatus='null', key=1}, 332=Employee{id=332, name='bSudhan', salary='2000$', recordStatus='null', key=2}}

Note for employees to print properly you need to add a toString() method to the class.

Person Class toString() Function:

@Override
public String toString() {
    return "Employee{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", salary='" + salary + '\'' +
            ", recordStatus='" + recordStatus + '\'' +
            ", key=" + key +
            '}';
}
John Humphreys
  • 37,047
  • 37
  • 155
  • 255
1

The easiest way to remove duplicate is by passing the List to a Set and Use Comparator to remove duplicate elements.

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class RemoveDuplicate {

    public static void main(String[] args) {

        List<Employee> list = new ArrayList<Employee>();
        list.add(new Employee(123, "zMadhu", "1000$",1));
        list.add(new Employee(332, "bSudhan", "2000$",2));
        list.add(new Employee(54, "cKongarass", "3000$",3));
        list.add(new Employee(54, "xKongarass", "3000$",4));
        list.add(new Employee(54, "aKongarass", "3000$",5));

        //Printing original list
        for (Employee emp : list) {
            System.out.println(emp.getId());
        }

        Set<Employee> set = new TreeSet<Employee>(new Comparator<Employee>() {

            @Override
            public int compare(Employee e1, Employee e2) {
                return e1.getId() == e2.getId() ? 0 : 1;
            }
        });
        set.addAll(list);

        final ArrayList<Employee> newList = new ArrayList<Employee>(set);

        System.out.println("\n***** After removing duplicates *******\n");
        for (Employee emp : newList) {
            System.out.println(emp.getId());
        }
    }

}
senjin.hajrulahovic
  • 2,961
  • 2
  • 17
  • 32
BSB
  • 19
  • 3
  • Hi All, Thanks for the Replies. I did the same where I created a comparator and added my own sorting logic to sort the ID. But I want to make sure that I want to delete by doing a combination of sorting like (ID + Name ) – madhu kongara Sep 12 '18 at 18:26
1

If you override the equals method accordingly you can do it this way in java 8+:

import java.util.stream.Collectors;

list.stream().distinct().collect(Collectors.toList())

It's also achievable without overriding the equals method, but more verbose though:

Set<Employee> uniqueSet = new TreeSet<>((e1, e2) -> e1.getId() == e2.getId() ? 0 : 1);
set.addAll(list);

List<Employee> result = new ArrayList<>(uniqueSet);

The lambda passed to the TreeSet constructor gets expanded to an implementation of Comparator<Employee>. Similar to the solution provided by @bsb but using java 8 features.

senjin.hajrulahovic
  • 2,961
  • 2
  • 17
  • 32
  • I was able to do with ID. But I want to do a sorting based on the Name too. Like in my example I have 54 with 3 names. But in the names I want to get the name DESC. Now I am facing issues on how to get that ID , NAME combination sorting and get the right ones. – madhu kongara Sep 12 '18 at 18:30
  • You can lookup sorting with multiple field here: https://stackoverflow.com/questions/4258700/collections-sort-with-multiple-fields – senjin.hajrulahovic Sep 12 '18 at 19:26