In this article, we will discuss how to remove duplicate elements from ArrayList
Q) How to convert ArrayList to HashSet ?
- use inter-conversion collection constructor
- Syntax :
Set<T> set = new HashSet<T>(alObj);
To remove duplicate elements from ArrayList :
Steps:
- Iterate through ArrayList to check duplicate elements
- Create HashSet using inter-conversion collection constructors
- Add ArrayList object to constructor argument
- Again, iterate through HashSet which stores only unique elements
- If NULL is present; maximum of one NULL element is allowed
ConvertArrayListToHashSet.java
package in.bench.resources.java.collections;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
public class ConvertArrayListToHashSet {
public static void main(String[] args) {
// creating ArrayList object of type String
ArrayList<String> fruits = new ArrayList<String>();
// adding elements to ArrayList object
fruits.add("Apple");
fruits.add("Greeplum");
fruits.add("Blackberry");
fruits.add("Apple"); // duplicate
fruits.add("Mango");
fruits.add("Orange");
fruits.add("Greeplum"); // duplicate
// Iterating using enhanced for-loop
System.out.println("ArrayList as per Insertion Order:\n");
for(String fruit : fruits) {
System.out.println(fruit);
}
// remove duplicates by converting to HashSet
// convert to HashSet
Collection<String> hsetFruits = new HashSet<String>(fruits);
// Iterating using enhanced for-loop
System.out.println("\n\nUnique Fruit of AL\n");
for(String name : hsetFruits) {
System.out.println(name);
}
}
}
Output:
ArrayList as per Insertion Order:
Apple
Greeplum
Blackberry
Apple
Mango
Orange
Greeplum
Unique Fruit of AL
Blackberry
Greeplum
Orange
Apple
Mango
From above example, ArrayList
- allows duplicate elements
- null object is allowed
- while iterating insertion-order is maintained
From above example, HashSet
- doesn’t allow duplicate elements
- maximum of one null object is allowed
- while iterating, retrieve elements in random-order
Related Articles:
- Java – Creating ArrayList using nCopies method of Collections class
- Java – How to get size or length of an ArrayList ?
- Java – Adding element to ArrayList at specified index position
- Java – Remove element from ArrayList at specified index position
- Java – How to delete a element and delete all elements of an ArrayList ?
- Java- How to get sublist from ArrayList ?
- Java – How to check whether particular element is present in ArrayList ?
- Java – Comparing two ArrayList objects using containsAll() method
- Java – Adding one ArrayList to another ArrayList using addAll() method
- Java – Replacing ArrayList element with new value using set() method
- Java – How to get maximum element from ArrayList ?
- Java – How to get minimum element from ArrayList ?
- Java – Various ways to iterate through ArrayList
- Java – How to reverse ArrayList contents ?
- Java – Sorting ArrayList in descending order
- Java 8 – Sorting ArrayList using sort() method of List
- Java – Sorting list of objects on multiple fields using Comparator
- Java – Remove duplicate elements from ArrayList
- Java – How to remove duplicate elements of ArrayList maintaining insertion-order ?
- 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/7/docs/api/java/util/ArrayList.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/class-use/HashSet.html
- https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html
Happy Coding !!
Happy Learning !!