javarevisited

javarevisited


How to remove duplicates elements from ArrayList in Java

Posted: 23 Dec 2012 01:31 AM PST

You can remove duplicates or repeated elements from ArrayList in Java by converting ArrayList into HashSet in Java. but before doing that just keep in mind that Set doesn't preserver insertion order which is guaranteed by List, in fact that's the main difference between List and Set in Java. So when you convert ArrayList to HashSet all duplicates elements will be removed but insertion order will be lost. Let's see this in action by writing a Java program to remove duplicates from ArrayList in Java. In this Java collection tutorial we will see both approach of deleting duplicates from ArrayList e.g using Hashset and LinkedHashSet and compare order of elements in final ArrayList which contains no duplicates. If you are not very familiar of What is an ArrayList and HashSet in Java collection framework, I suggest reading Java ArrayList Example and 10 HashSet Example in Java. These articles contains good introduction of most common used collection in Java i.e. ArrayList and HashSet.
Read more »


Post a Comment