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
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago