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
![How to remove duplicate object from ArrayList in Java](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgH9En1hzFynvedU3LsRlVOLzDWC0JTMNPbG67U-80M4FvoCWPOmo4nmW0-kkGbD8hLuHhuJuBIQXEGn1twUHQubkJ9XBCXslB7LtaApRP8E3UDYpT_wxAxxkkYgupI1Zk1xignlBDMOTRq/s1600/17.jpg)
//ArrayList with duplicates String
List<String> duplicateList = (List<String>) Arrays.asList("Android" , "Android", "iOS", "Windows mobile");
System.out.println("size of Arraylist with duplicates: " + duplicateList.size()); //should print 4 becaues of duplicates Android
System.out.println(duplicateList);
//Converting ArrayList to HashSet to remove duplicates
HashSet<String> listToSet = new HashSet<String>(duplicateList);
//Creating Arraylist without duplicate values
List<String> listWithoutDuplicates = new ArrayList<String>(listToSet);
System.out.println("size of ArrayList without duplicates: " + listToSet.size()); //should print 3 becaues of duplicates Android removed
System.out.println(listWithoutDuplicates);
Output:
size of Arraylist with duplicates: 4
[Android, Android, iOS, Windows mobile]
size of ArrayList without duplicates: 3
[Android, Windows mobile, iOS]
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:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
public class RemoveDuplicatesFromArrayList {
public static void main(String args[]) {
//ArrayList with duplicates String
List<String> duplicateList = (List<String>) Arrays.asList("Android" , "Android", "iOS", "Windows mobile");
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
public class RemoveDuplicatesFromArrayList {
public static void main(String args[]) {
//ArrayList with duplicates String
List<String> duplicateList = (List<String>) Arrays.asList("Android" , "Android", "iOS", "Windows mobile");
//should print 4 becaues of duplicates Android
System.out.println("size of Arraylist with duplicates: " +
duplicateList.size());
System.out.println("ArrayList with duplicates: " + duplicateList);
//Converting ArrayList to HashSet to remove duplicates
LinkedHashSet<String> listToSet = new LinkedHashSet<String>(duplicateList);
//Creating Arraylist without duplicate values
List<String> listWithoutDuplicates = new ArrayList<String>(listToSet);
System.out.println("ArrayList with duplicates: " + duplicateList);
//Converting ArrayList to HashSet to remove duplicates
LinkedHashSet<String> listToSet = new LinkedHashSet<String>(duplicateList);
//Creating Arraylist without duplicate values
List<String> listWithoutDuplicates = new ArrayList<String>(listToSet);
//should print 3 becaues of duplicates Android removed
System.out.println("size of ArrayList without duplicates: " + listToSet.size());
System.out.println("ArrayList after removing duplicates in same order: " + listWithoutDuplicates);
}
}
Output:
size of Arraylist with duplicates: 4
ArrayList with duplicates: [Android, Android, iOS, Windows mobile]
size of ArrayList without duplicates: 3
ArrayList after removing duplicates in same order: [Android, iOS, Windows mobile]
System.out.println("size of ArrayList without duplicates: " + listToSet.size());
System.out.println("ArrayList after removing duplicates in same order: " + listWithoutDuplicates);
}
}
Output:
size of Arraylist with duplicates: 4
ArrayList with duplicates: [Android, Android, iOS, Windows mobile]
size of ArrayList without duplicates: 3
ArrayList after removing duplicates in same order: [Android, iOS, Windows mobile]
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
from ArrayList. 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.
Related Java ArrayList tutorials from this Blog
No comments:
Post a Comment