Pages

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.

Java program to delete duplicates from
ArrayList

Here is a quick example of removing duplicates from ArrayList in Java. Suppose
you have an ArrayList of String which contains 4 Strings out of those one is duplicate and we
want to remove that duplicate:

Now if you have noticed here duplicate entry "Android" has been
removed from ArrayList but order of ArrayList is not
same. Since we have converted ArrayList to HashSet we have
lost insertion order of elements. but don't worry there is another way of
removing duplicates from ArrayList without losing order of elements,
for that instead of HashSet we need to use LinkedHashSet,
Which guarantees insertion order. Just remember checking
whether ArrayList contains
duplicates or not is completely different than removing it, which is what
we are doing here. Here is a another example of removing duplicate entries from
ArrayList without losing insertion order or entries:

So now we know that how to remove duplicates from ArrayList in Java
and also knows how to preserver order of element while removing duplicates
fromArrayList. If you don't prefer converting
List to Set than you can still go with copying data from
one ArrayList to other ArrayList and
removing duplicates by checking with ArrayList.contains() method.

Hi @Rohan and @Tomasz, There are many times when you are presented with a legacy code and you don't write everything by scratch. Sometime with gap in requirement or change of business requirement this kind of things creeps into code. I am firm believer to fix root cause but it's not always possible so work around come in place. If you have a ArrayList which can contain duplicates but you need to operate with only unique elements with same order, then you need to remove those duplicates.

@Avinash, to remove duplicates from the list w/o using another list, use an iterator and a temporary set.Set seen = HashSet();Iterator it = myObject.iterator();while (it.hasNext()) { final MyObject o = it.getNext(); if (seen.contains(o)) { it.remove(o); } else { seen.add(o); }}

Anyway even when interfacing with legacy code, I'd still expect it to be a List not some concrete implementation being passed around. Also are you sure the cast is needed with '(List) Arrays.asList(..)' ?