<div dir="ltr" style="text-align: justify;" trbidi="on">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&#8217;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. </p>
<div align="center" id="ads-id"></div>
<p>
For being familiar with an <a href="https://dineshonjava.com/2013/05/arraylist-class-in-java-collection.html"><b>ArrayList </b></a>and <a href="https://dineshonjava.com/2013/05/hashset-class-in-collection.html"><b>HashSet </b></a>in <a href="https://dineshonjava.com/2013/01/core-java-baby-step-to-be-best-java-ian.html#Collection_Classes"><b>Java collection framework</b></a>, I suggest reading <a href="https://dineshonjava.com/2013/01/core-java-baby-step-to-be-best-java-ian.html#Collection_Classes"><b>Java Collection Framework</b></a> and <a href="https://dineshonjava.com/2013/05/arraylist-class-in-java-collection.html"><b>ArrayList </b></a>and <a href="https://dineshonjava.com/2013/05/hashset-class-in-collection.html"><b>HashSet </b></a>Example in Java. </p>
<p><b>Popular Tutorials</b></p>
<table align="center" bgcolor="#FFFFFF" border="1" bordercolor="#000000">
<tbody>
<tr>
<th scope="col"><a href="https://dineshonjava.com/2012/06/spring-30-baby-step-to-learn.html"><em><strong>Spring Tutorial</strong> </em></a></th>
<th scope="col"><a href="https://dineshonjava.com/2012/12/spring-web-mvc-framework-chapter-38.html"><strong><em>Spring MVC Web Tutorial </em></strong></a></th>
<th scope="col"><a href="https://dineshonjava.com/2016/06/introduction-to-spring-boot-a-spring-boot-complete-guide.html"><em><strong>Spring Boot Tutorial</strong> </em></a></th>
</tr>
<tr>
<th scope="col"><strong><a href="https://dineshonjava.com/2013/02/spring-security-take-baby-step-to-secure.html"><em>Spring Security Tutorial</em></a></strong></th>
<th scope="col"><strong><a href="https://dineshonjava.com/2012/07/introduction-to-aop-in-spring.html"><em>Spring AOP Tutorial</em></a></strong></th>
<th scope="col"><strong><a href="https://dineshonjava.com/2012/12/using-spring-jdbc-framework-chapter-32.html"><em>Spring JDBC Tutorial</em></a></strong></th>
</tr>
<tr>
<th scope="col"><a href="https://dineshonjava.com/2017/01/spring-hateoas-hypermedia-driven-restful-web-service.html"><em><strong>Spring HATEOAS </strong></em></a></th>
<th scope="col"><a href="https://dineshonjava.com/2017/01/microservices-with-spring-boot.html"><em><strong>Microservices with Spring Boot</strong></em></a></th>
<th scope="col"><a href="https://dineshonjava.com/2013/06/jax-rs-web-service-tutorial.html"><strong><em>REST Webservice</em> </strong></a></th>
</tr>
<tr>
<th scope="col"><a href="https://dineshonjava.com/2013/01/core-java-baby-step-to-be-best-java-ian.html"><em><strong>Core Java </strong></em></a></th>
<th scope="col"><a href="https://dineshonjava.com/2012/03/hibernate-3-on-baby-steps.html"><em><strong>Hibernate Tutorial</strong></em></a></th>
<th scope="col"><a href="https://dineshonjava.com/2012/12/spring-batch-process-with-example.html"><strong><em>Spring Batch</em> </strong></a></th>
</tr>
</tbody>
</table>
<p>
<b>Example for Removing Duplicate Items from ArrayList</b><br />
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&#8217;s see in the below java code.</p>
<pre class="highlight">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);

 }
}

</pre>
<p>
<b>Output:</b></p>
<p><i>size of Arraylist with duplicates: 5</i><br />
<i>ArrayList with duplicates: [DOJ, DOJ, MB, DR, AM]</i><br />
<i>size of ArrayList without duplicates: 4</i><br />
<i>ArrayList after removing duplicates in same order: [DR, DOJ, AM, MB]</i></p>
<p>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&#8217;s see below example of java code.</p>
<pre class="highlight">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);

 }
}

</pre>
<p>
<b>Output</b><br />
<i>size of Arraylist with duplicates: 5</i><br />
<i>ArrayList with duplicates: [DOJ, DOJ, MB, DR, AM]</i><br />
<i>size of ArrayList without duplicates: 4</i><br />
<i>ArrayList after removing duplicates in same order: [DOJ, MB, DR, AM]</i></p>
<p>Now according above output of the program we have removed duplicate items and also maintain our insertion order of this list.</p>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/core-java-interview-questions/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/why-to-void-comparing-integer-using-double-equals-operator-in-java5/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '76'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…