2 Replies Latest reply on Oct 21, 2018 6:44 AM by Tharaka Uchiha

    Question on removing duplicate data in a collection

    816802

      Hi All,

       

      I have the below employee class defined.

       

      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.

       

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

        • 1. Re: Question on removing duplicate data in a collection
          Shivakumar Ayyanagowdar-Oracle

          If you do not want duplicates to be present, then use Hashset/set

          • 2. Re: Question on removing duplicate data in a collection
            Tharaka Uchiha

            I hope this will help u

             

            package com.domin;

             

             

            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;

             

             

            }

             

             

            public int getId() {

            return id;

            }

             

             

            public void setId(int id) {

            this.id = id;

            }

             

             

            public String getName() {

            return name;

            }

             

             

            public void setName(String name) {

            this.name = name;

            }

             

             

            public String getSalary() {

            return salary;

            }

             

             

            public void setSalary(String salary) {

            this.salary = salary;

            }

             

             

            public String getRecordStatus() {

            return recordStatus;

            }

             

             

            public void setRecordStatus(String recordStatus) {

            this.recordStatus = recordStatus;

            }

             

             

            public int getKey() {

            return key;

            }

             

             

            public void setKey(int key) {

            this.key = key;

            }

             

             

            @Override

            public String toString() {

            return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + ", recordStatus=" + recordStatus

            + ", key=" + key + "]";

            }

             

            @Override

            public int compareTo(Employee o) {

             

            int currentval= this.getId();

            int newval=o.getId();

             

            if(currentval>newval){

            return 1;

            }else{

            return -1;

            }

             

            }

             

            }

             

            -------------------------------------------------------------------------------------

             

             

            public static void main(String[] args) {

             

            boolean flag=false;

             

            List<Employee> list = new ArrayList<>();

            TreeSet<Employee> newlist = new TreeSet<>();

             

            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));

             

            for (Employee employee : list) {

            flag=false;

            for (Employee employee2 : newlist) {

            if(employee.getId()==employee2.getId()){

            flag=true;

            }

             

            }

            if(!flag){

            newlist.add(employee);

            }

             

            }

             

            for (Employee employee2 : newlist) {

            System.out.println(employee2);

             

            }

             

             

            }