Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

remove duplicates from ArrayList

807591Jun 24 2005 — edited Jun 20 2008
Hi,

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

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 18 2008
Added on Jun 24 2005
16 comments
495 views