For being familiar with an ArrayList and HashSet in Java collection framework, I suggest reading Java Collection Framework and ArrayList and HashSet Example in Java.
Popular Tutorials
Example for Removing Duplicate Items from ArrayList
It very simple example for removing duplicate items from ArrayList. Here I have an ArrayList with 5 items String type. This ArrayList has some duplicate items. Let’s see in the below java code.
import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; /** * */ /** * @author Dinesh.Rajput * */ public class DuplicateItemsRemove { /** * @param args */ public static void main(String[] args) { //ArrayList with duplicates items List<String> listOfDuplicateItems = (List<String>) Arrays.asList("DOJ" , "DOJ", "MB", "DR", "AM"); //should print 5 with duplicates items System.out.println("size of Arraylist with duplicates: " + listOfDuplicateItems.size()); System.out.println("ArrayList with duplicates: " + listOfDuplicateItems); //Converting ArrayList to HashSet to remove duplicates Set<String> listToSet = new HashSet<String>(listOfDuplicateItems); //Creating Arraylist without duplicate items List<String> listOfItems = new ArrayList<String>(listToSet); //should print 4 because of duplicates items are removed System.out.println("size of ArrayList without duplicates: " + listToSet.size()); System.out.println("ArrayList after removing duplicates in same order: " + listOfItems); } }
Output:
size of Arraylist with duplicates: 5
ArrayList with duplicates: [DOJ, DOJ, MB, DR, AM]
size of ArrayList without duplicates: 4
ArrayList after removing duplicates in same order: [DR, DOJ, AM, MB]
According above output of the program we have removed duplicate items but also we have lost our insertion order of this list because I have converted ArrayList to HashSet. We can maintain our insertion order as well if we use LinkedHashSet instead of HashSet, Which guarantees insertion order. Let’s see below example of java code.
import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; /** * */ /** * @author Dinesh.Rajput * */ public class DuplicateItemsRemove { /** * @param args */ public static void main(String[] args) { //ArrayList with duplicates items List<String> listOfDuplicateItems = (List<String>) Arrays.asList("DOJ" , "DOJ", "MB", "DR", "AM"); //should print 5 with duplicates items System.out.println("size of Arraylist with duplicates: " + listOfDuplicateItems.size()); System.out.println("ArrayList with duplicates: " + listOfDuplicateItems); //Converting ArrayList to LinkedHashSet to remove duplicates Set<String> listToSet = new LinkedHashSet<String>(listOfDuplicateItems); //Creating Arraylist without duplicate items List<String> listOfItems = new ArrayList<String>(listToSet); //should print 4 because of duplicates items are removed System.out.println("size of ArrayList without duplicates: " + listToSet.size()); System.out.println("ArrayList after removing duplicates in same order: " + listOfItems); } }
Output
size of Arraylist with duplicates: 5
ArrayList with duplicates: [DOJ, DOJ, MB, DR, AM]
size of ArrayList without duplicates: 4
ArrayList after removing duplicates in same order: [DOJ, MB, DR, AM]
Now according above output of the program we have removed duplicate items and also maintain our insertion order of this list.