943,948 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 42834
  • Java RSS
Sep 21st, 2005
0

removing duplicates in arraylist

Expand Post »
hi forum,
i am using an arraylist for storing a list of objects.i dont want the objects in the list to be ordered but the list should be duplicate free.how can i achieve this.plz help quickly.provide source code if possible.thanks in advance.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Dark Master is offline Offline
8 posts
since Aug 2005
Sep 21st, 2005
0

Re: removing duplicates in arraylist

A quick and dirty way of doing it would be to create a new ArrayList (arrList2), use a loop to read each object in the original ArrayList (arrList1), use the indexOf method to check arrList2 for the presence of the object that you are checking, add the object to arrList2 if the object isn't present in the list, and replace the ArrayList instance in arrList1 with arrList2.
Reputation Points: 38
Solved Threads: 25
Posting Shark
chrisbliss18 is offline Offline
902 posts
since Aug 2005
Sep 21st, 2005
0

Re: removing duplicates in arraylist

If you don't want duplicates, why not use a Set. Like the HashSet. A Set enforces no duplicates.

For further information on all Java Collections, please read:
http://java.sun.com/docs/books/tutor...ons/index.html
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Kate Albany is offline Offline
71 posts
since Jun 2005
Sep 21st, 2005
0

Re: removing duplicates in arraylist

There is also a contains(Object o) method on List. So, you could check to see if the object is in the ArrayList before adding the Object.

Or you could extend ArrayList with your own class that checks to see if Object is present in the ArrayList using the contains method and then adding the new object only if it isn't present in the current list.

I actually would recommend using Kate's advice though.

Regards,

Nate
Reputation Points: 11
Solved Threads: 8
Posting Whiz in Training
hooknc is offline Offline
216 posts
since Aug 2005
Sep 21st, 2005
0

Re: removing duplicates in arraylist

Hashset is an excellent idea, but if you want to just use arraylists then the contain method hook mentioned is the way to go.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Sep 22nd, 2005
0

Re: removing duplicates in arraylist

Using contains would indeed work, but if the List gets large it gets ever more expensive to do the check (it's an O(n) operations, iterating over the entire dataset until either the end is reached or a match is found).
Using a container that prohibits duplicates is indeed a more logical choice.
HashSet checks for duplicates during insert based on hashCode and equals (so make sure to correctly override both for your custom classes when using them in a HashSet) which is a lot faster potentially.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Sep 22nd, 2005
0

Re: removing duplicates in arraylist

thanks u guys for ur quick replys. i m intending to replace the arraylist collection by an hashSet collection.can anyone provide code to implement this.i m new to java and so i didnot understand the equals and hashcode overridding stuff that jwenting mentioned.any light on this ???? thanks again.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Dark Master is offline Offline
8 posts
since Aug 2005
Sep 22nd, 2005
0

Re: removing duplicates in arraylist

hascCode is used in concert with equals to determine whether (and where) in a HashSet (or other hash based collection) an object exists.
You need to have these methods in working order (and thus overridden from the defaults in Object) in order for the algorithms to work correctly.

There's a good explanation in "Effective Java" by Joshua Bloch.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Apr 12th, 2010
-1
Re: removing duplicates in arraylist
hi, guys
where i have a situation that is regarding session. i opened 10 browsers with same user and password.
if i had make any changes to 1 st broser, then it will effect those changes in another browsers.
what happens if i close the 1 st browser and try to use the session of 1 st browser in 5th browser?
my mail: <EMAIL SNIPPED>
Last edited by peter_budo; Apr 12th, 2010 at 3:28 am. Reason: Keep It On The Site - Do not post asking for an answer to be sent to you via email or PM.
Reputation Points: 8
Solved Threads: 0
Newbie Poster
pnr.nagi@gmail. is offline Offline
1 posts
since Apr 2010
Apr 12th, 2010
0
Re: removing duplicates in arraylist
don't hijack other peoples' threads.
don't revive long dead threads.
do your own homework.
don't ask for private help by email.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: boolean array help
Next Thread in Java Forum Timeline: directory traversal please help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC