How to remove duplicate items from ArrayList in Java?

We can remove duplicate items from ArrayList in java by simply converting ArrayList into Set in Java. But Set does not maintain insertion order as by List. So whenever we will convert ArrayList to HashSet then insertion order will be lost. Let’s discuss these thing with example so we are writing java program with main method which contain an ArrayList with duplicate items. In this java collection tutorial we will see both approaches of deleting duplicates from ArrayList e.g using Hashset and LinkedHashSet and compare order of elements in final ArrayList which contains no duplicates.

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

Spring Tutorial Spring MVC Web Tutorial Spring Boot Tutorial
Spring Security Tutorial Spring AOP Tutorial Spring JDBC Tutorial
Spring HATEOAS Microservices with Spring Boot REST Webservice
Core Java Hibernate Tutorial Spring Batch

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.

Previous
Next