remove duplicates from ArrayList
807591Jun 24 2005 — edited Jun 20 2008Hi,
I am sorry for asking a simple question.
I have an ArrayList which contains Location objects.
Location is a bean class with LocationId, Name, Address etc..
Now I want to remove duplicates from this ArrayList.
We know whether duplicates exist or not by checking LocationId of Location object. LocationId is int.
How can I remove duplicates from this ArrayList?
I tried using contains method of ArrayList.
I even tried to copy all the values to HashSet
(HashSet h = new HashSet();
h.addAll(arrayList);
arrayList.clear();
arrayList.addAll(h); )
but as ArrayList contains Location objects, it can not detect duplicate elements.
Thanks in advance
nmadhava