removing duplicates in arraylist

Reply

Join Date: Aug 2005
Posts: 8
Reputation: Dark Master is an unknown quantity at this point 
Solved Threads: 0
Dark Master Dark Master is offline Offline
Newbie Poster

removing duplicates in arraylist

 
0
  #1
Sep 21st, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 902
Reputation: chrisbliss18 is an unknown quantity at this point 
Solved Threads: 23
chrisbliss18's Avatar
chrisbliss18 chrisbliss18 is offline Offline
Posting Shark

Re: removing duplicates in arraylist

 
0
  #2
Sep 21st, 2005
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.
Did we help you? Did we miss the point entirely? Update your thread and let us know.
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 71
Reputation: Kate Albany is an unknown quantity at this point 
Solved Threads: 1
Kate Albany Kate Albany is offline Offline
Junior Poster in Training

Re: removing duplicates in arraylist

 
0
  #3
Sep 21st, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 216
Reputation: hooknc is an unknown quantity at this point 
Solved Threads: 8
hooknc hooknc is offline Offline
Posting Whiz in Training

Re: removing duplicates in arraylist

 
0
  #4
Sep 21st, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: removing duplicates in arraylist

 
0
  #5
Sep 21st, 2005
Hashset is an excellent idea, but if you want to just use arraylists then the contain method hook mentioned is the way to go.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: removing duplicates in arraylist

 
0
  #6
Sep 22nd, 2005
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 8
Reputation: Dark Master is an unknown quantity at this point 
Solved Threads: 0
Dark Master Dark Master is offline Offline
Newbie Poster

Re: removing duplicates in arraylist

 
0
  #7
Sep 22nd, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: removing duplicates in arraylist

 
0
  #8
Sep 22nd, 2005
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC