| | |
removing duplicates in arraylist
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
•
•
Join Date: Jun 2005
Posts: 71
Reputation:
Solved Threads: 1
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
For further information on all Java Collections, please read:
http://java.sun.com/docs/books/tutor...ons/index.html
•
•
Join Date: Aug 2005
Posts: 216
Reputation:
Solved Threads: 8
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
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
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.
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.
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.
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.
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: Java1 or Java2?
- Next Thread: problems with HashSet
Views: 17666 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for Java
6 android api apple applet application arguments array arrays automation binary bluetooth bold byte c++ chat class classes client code component coordinates database datagram doctype draw eclipse educational error event exception file fractal froglogic game givemetehcodez graphics gui helpwithhomework html ide ideas image ingres input integer internet intersect ip j2me java javaexcel javaprojects jmf jni jpanel jtextarea julia linux list loop map method methods mobile netbeans newbie nextline number object oracle pong print problem program programming project recursion recursive scanner screen sell server set size sms socket sort sql string swing test threads time transfer tree user web websites windows






