In this article, we will discuss how to remove duplicate elements from ArrayList maintaining its insertion-order
1. Solution:
- simply, convert to LinkedHashSet
2. Remove duplicate elements from ArrayList :
Steps:
- Iterate through ArrayList to understand insertion-order
- Create LinkedHashSet using inter-conversion collection constructors
- Add ArrayList object to constructor argument
- Again, iterate through LinkedHashSet which maintains insertion-order removing duplicate elements from ArrayList
ArrayListPushToLinkedHashSet.java
package in.bench.resources.java.collections;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
public class ArrayListPushToLinkedHashSet {
public static void main(String[] args) {
// creating ArrayList object of type String
ArrayList<String> techCompanies = new ArrayList<String>();
// adding elements to ArrayList object
techCompanies.add("Sun");
techCompanies.add("Apple");
techCompanies.add("JBoss");
techCompanies.add("Whatsup");
techCompanies.add("Apple");
techCompanies.add("BEA Weblogic");
techCompanies.add("JBoss");
// Iterating using enhanced for-loop
System.out.println("ArrayList as per Insertion Order:\n");
for(String techCompany : techCompanies) {
System.out.println(techCompany);
}
// remove duplicates, maintaining insertion order
// convert to LinkedHashSet
Collection<String> lhs = new LinkedHashSet<String>(techCompanies);
// Iterating using enhanced for-loop
System.out.println("\n\nUnique elements and Insertion order\n");
for(String techCompany : lhs) {
System.out.println(techCompany);
}
}
}
Output:
ArrayList as per Insertion Order:
Sun
Apple
JBoss
Whatsup
Apple
BEA Weblogic
JBoss
Unique elements and Insertion order
Sun
Apple
JBoss
Whatsup
BEA Weblogic
Note:
- There is another way to remove duplicate elements from ArrayList
- without using any Collection classes
- Check Remove duplicate elements from ArrayList in Java with example/explanation
Related Articles:
- Java – How to get size or length of HashSet ?
- Java – How to delete a element and delete all elements of HashSet ?
- Java – retainAll() method explanation with HashSet
- Java – How to check whether particular element is present in HashSet ?
- Java – Comparing two HashSet objects using containsAll() method
- Java – Adding one HashSet to another HashSet using addAll() method
- Java – Various ways to iterate through HashSet
- Java – How to reverse LinkedHashSet contents ?
- Java – Sorting HashSet contents in ascending and descending order
- Java – How to Sort HashSet in 2 ways ?
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
- https://docs.oracle.com/javase/tutorial/collections/implementations/list.html
- https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
- https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html
- https://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashSet.html
Happy Coding !!
Happy Learning !!