| | |
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.
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
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class client code compile compiler component database development digit eclipse equation error event fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying pearl problem program programming project qt recursion scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver string superclass swing system text-file thread threads tree variablebinding windows xor






