0

How can I remove all objects in a list that have the same ID?

For example in the following list:

    Person person1 = new Person();
    person1.setId("1");

    Person person2 = new Person();
    person2.setId("1"); //ie same as Person1

    Person person3 = new Person();
    person3.setId("2");

    List<Person> PersonList = new ArrayList<>();
    PersonList.add(person1);
    PersonList.add(person2);
    PersonList.add(person3);

My Method below works for the above but not when there are multiple duplicates, how can I solve this case also?

public List<Person> removeDuplicatesFromList(List<Person> personList){

        for (int i = 0; i < personList.size(); i++) {
            for (int j = i+1; j < personList.size(); j++) {

                if(personList.get(i).getId().equalsIgnoreCase(personList.get(j).getId())){

                    personList.remove(personList.get(j));
                }else{

                }
            }
        }

        return personList;
    }
java123999
  • 6,974
  • 36
  • 77
  • 121
  • Possible duplicate of [Java 8 Distinct by property](https://stackoverflow.com/questions/23699371/java-8-distinct-by-property) – daniu Feb 08 '18 at 15:09
  • Why not using a HashSet ? Override "equals" on Person to make a comparaison based on Id. Then you would'nt have duplicates. – olikaf Feb 08 '18 at 15:10
  • 1
    @olikaf Why override equals (probably a very bad idea) if you can just use a `Map`? – tobias_k Feb 08 '18 at 15:13
  • You sohuld not remove elements from a list while you are iterating over it! – Stimpson Cat Feb 08 '18 at 15:14
  • Use a set and it will automatically do it or use Lambda Expressionsµ – Luai Ghunim Feb 08 '18 at 15:17
  • @LuaiGhunim please explain via example – java123999 Feb 08 '18 at 15:19
  • @java123999 Java8 introdcued new concepts of stream and lambda expression,derived from haskell. Check this example `https://stackoverflow.com/questions/45418738/remove-duplicate-from-list-java8` Here people have explained , how to removed duplicates without loop and that is also beautiful – Luai Ghunim Feb 08 '18 at 15:22
  • @tobias_k you can override equals if it make sense to compare Person by IDs. Same IDs, then they are equals. But in fact HashSet use hashCode() :-). But then again, same IDs should implies same hash code... – olikaf Feb 08 '18 at 16:27

2 Answers2

2

Like @tobias_k already commented, I suggest you to use Map<String, Person> instead of List<Person>. This will avoid duplicates in the first place, so that there is no need to remove them.

Your modified code would like this:

Person person1 = new Person();
person1.setId("1");

Person person2 = new Person();
person2.setId("1"); //i.e. same as person1

Person person3 = new Person();
person3.setId("2");

Map<String, Person> personMap = new HashMap<>();
personMap.put(person1.getId(), person1);
personMap.put(person2.getId(), person2);
personMap.put(person3.getId(), person3);

Calling personMap.get("1") then would give person2, and personMap.get("2") would give person3.

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
1

You can make use of Map. Use key as person id and person object as value

public List<Person> removeDuplicatesFromList(List<Person> personList){
   Map<String, Person> map = new HashMap<String, Person>();
   List<Person> newPersonList = new ArrayList<>();
   for(Person p:personList){
       map.put(p.getId(),p);
   }
   Iterator itr=map.keySet().iterator();
   while (itr.hasNext()) {
     String id =  itr.next().toString();
     newPersonList.add((Person)map.get(id));
    }
  return newPersonList;
 }
thebishal
  • 71
  • 6