how to remove duplicate objects from java arraylist

13.2k Views Asked by At

I have requirement to remove the duplicate object from my arraylist of object. I tried it by making to arraylist of objects. First one contains all objects including duplicate and the another contain only the unique one.

ArrayList<ListTableClass> ltc = new ArrayList<ListTableClass>();//has duplicate
ArrayList<ListTableClass> ltc2 = new ArrayList<ListTableClass>();//unique

And i used contains method to check for dublicates like this:

for (ListTableClass element : ltc) {
          if (!ltc2.contains(element)) {
            ltc2.add(element);
          }
    }

but this does not remove duplicates. It adds all the elements of ltc to ltc2. Don't know why? ltc does contain duplicate objects.

1

There are 1 best solutions below

0
On BEST ANSWER

Because the function "contains" of ArrayList compare with two objects in their functions hashcode & equals, so you must override the function "hashCode" & "equals" of Class ListTableClass.

example:

import java.util.ArrayList;
public class ListTableClass {
    private String name;
    private int age;

public ListTableClass(String name, int age) {
    super();
    this.name = name;
    this.age = age;
}

public static void main(String[] args) {
    ArrayList<ListTableClass> ltc = new ArrayList<ListTableClass>();// has duplicate
    ListTableClass obj0 = new ListTableClass("A", 0);
    ListTableClass obj1 = new ListTableClass("B", 1);
    ListTableClass obj2 = new ListTableClass("C", 2);
    ListTableClass obj3 = new ListTableClass("A", 0);
    ltc.add(obj0);
    ltc.add(obj1);
    ltc.add(obj2);
    ltc.add(obj3);
    ArrayList<ListTableClass> ltc2 = new ArrayList<ListTableClass>();// unique
    for (ListTableClass element : ltc) {
        if (!ltc2.contains(element)) {
            System.out.println(element);
            ltc2.add(element);
        }
    }
}
}

output before override hashCode:

y2015.m06.d10.ListTableClass@659e0bfd //obj0 {"A", 0}
y2015.m06.d10.ListTableClass@2a139a55 //obj1 {"B", 1}
y2015.m06.d10.ListTableClass@15db9742 //obj2 {"C", 2}
y2015.m06.d10.ListTableClass@6d06d69c //obj3 {"A", 0} is different from obj0

add override function hashCode:

@Override
 public int hashCode() {
 final int prime = 31;
 int result = 1;
 result = prime * result + age;
 result = prime * result + ((name == null) ? 0 : name.hashCode());
 return result;
 }

    @Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    ListTableClass other = (ListTableClass) obj;
    if (age != other.age)
        return false;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    return true;
}

then output is:

y2015.m06.d10.ListTableClass@402 //obj0
y2015.m06.d10.ListTableClass@422 //obj1
y2015.m06.d10.ListTableClass@442 //obj2

there is not obj3 because of obj3's hashcode & its property is equal with obj0