import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

public class RemoveDuplicatesFromArrayList {

public static void main(String[] args) {
ArrayList i = new ArrayList();
i.add(1);
i.add(3);
i.add(1);
i.add(3);
i.add(2);
Set s = removeDups(i);
for (Object object : s) {
System.out.println(object.toString()
);
}
System.out.println("end");
System.out.println();
}

public static Set removeDups(ArrayList al)
{
Set hs = new LinkedHashSet();
hs.addAll(al);
al.clear();
al.addAll(hs);
return hs;

}
}

Output:

1
3
2
end

Advertisements