You tin withdraw duplicates or repeated elements from ArrayList inward Java past times converting ArrayList into HashSet inward Java. but earlier doing that simply boot the bucket on inward heed that Set doesn't preserver insertion gild which is guaranteed past times List, in fact that’s the top dog difference betwixt List together with Set inward Java. So when you lot convert ArrayList to HashSet all duplicates elements volition live removed but insertion gild volition live lost. Let’s run across this inward activity past times writing a Java plan to withdraw duplicates from ArrayList inward Java. In this Java collection tutorial nosotros volition run across both approach of deleting duplicates from ArrayList e.g using Hashset together with LinkedHashSet together with compare gild of elements inward lastly ArrayList which contains no duplicates. If you lot are non real familiar of What is an ArrayList together with HashSet inward Java collection framework, I propose reading Java ArrayList Example together with 10 HashSet Example inward Java. These articles contains proficient introduction of around mutual used collection inward Java i.e. ArrayList together with HashSet.
Java plan to delete duplicates from ArrayList
Here is a quick event of removing duplicates from ArrayList inward Java. Suppose you lot accept an ArrayList of String which contains iv Strings out of those ane is duplicate together with nosotros desire to withdraw that duplicate:
//ArrayList amongst duplicates String
List<String> duplicateList = (List<String>) Arrays.asList("Android" , "Android", "iOS", "Windows mobile");
System.out.println("size of Arraylist amongst duplicates: " + duplicateList.size()); //should impress iv becaues of duplicates Android
System.out.println(duplicateList);
//Converting ArrayList to HashSet to withdraw 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 impress three becaues of duplicates Android removed
System.out.println(listWithoutDuplicates);
Output:
size of Arraylist amongst duplicates: 4
[Android, Android, iOS, Windows mobile]
size of ArrayList without duplicates: 3
[Android, Windows mobile, iOS]
Now if you lot accept noticed hither duplicate entry "Android" has been removed from ArrayList but order of ArrayList is non same. Since nosotros accept converted ArrayList to HashSet nosotros accept lost insertion gild of elements. but don't worry at that topographic point is simply about other agency of removing duplicates from ArrayList without losing gild of elements, for that instead of HashSet nosotros take to piece of occupation LinkedHashSet, Which guarantees insertion order. Just think checking whether ArrayList contains duplicates or not is completely dissimilar than removing it, which is what nosotros are doing here. Here is a simply about other event of removing duplicate entries from ArrayList without losing insertion gild 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 amongst 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 amongst duplicates String
List<String> duplicateList = (List<String>) Arrays.asList("Android" , "Android", "iOS", "Windows mobile");
//should impress iv becaues of duplicates Android
System.out.println("size of Arraylist amongst duplicates: " + duplicateList.size());
System.out.println("ArrayList amongst duplicates: " + duplicateList);
//Converting ArrayList to HashSet to withdraw duplicates
LinkedHashSet<String> listToSet = new LinkedHashSet<String>(duplicateList);
//Creating Arraylist without duplicate values
List<String> listWithoutDuplicates = new ArrayList<String>(listToSet);
System.out.println("ArrayList amongst duplicates: " + duplicateList);
//Converting ArrayList to HashSet to withdraw duplicates
LinkedHashSet<String> listToSet = new LinkedHashSet<String>(duplicateList);
//Creating Arraylist without duplicate values
List<String> listWithoutDuplicates = new ArrayList<String>(listToSet);
//should impress three becaues of duplicates Android removed
System.out.println("size of ArrayList without duplicates: " + listToSet.size());
System.out.println("ArrayList later removing duplicates inward same order: " + listWithoutDuplicates);
}
}
Output:
size of Arraylist amongst duplicates: 4
ArrayList amongst duplicates: [Android, Android, iOS, Windows mobile]
size of ArrayList without duplicates: 3
ArrayList later removing duplicates inward same order: [Android, iOS, Windows mobile]
System.out.println("size of ArrayList without duplicates: " + listToSet.size());
System.out.println("ArrayList later removing duplicates inward same order: " + listWithoutDuplicates);
}
}
Output:
size of Arraylist amongst duplicates: 4
ArrayList amongst duplicates: [Android, Android, iOS, Windows mobile]
size of ArrayList without duplicates: 3
ArrayList later removing duplicates inward same order: [Android, iOS, Windows mobile]
So directly nosotros know that how to withdraw duplicates from ArrayList inward Java together with likewise knows how to preserver gild of chemical component division spell removing duplicates from ArrayList. If you lot don't prefer converting List to Set than you lot tin nonetheless boot the bucket amongst copying information from ane ArrayList to other ArrayList together with removing duplicates past times checking amongst ArrayList.contains() method.
Further Learning
Java In-Depth: Become a Complete Java Engineer
How to loop or iterate over ArrayList inward Java