Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java ArrayList to HashSet Conversion

In this post, we are going to convert ArrayList to HashSet in Java. ArrayList is an implementation class of List interface whereas HashSet is an implementation class of Set interface.

If we want to get a collection of unique elements then we can use HashSet. The conversion of ArrayList to HashSet will remove all the duplicate elements of the ArrayList.

Although there are several ways to convert ArrayList to HashSet, here we are using some common ways like using add() method or HashSet constructor or Stream API for the latest java version.

Time for an Example:

Let's take an example to get HashSet from ArrayList. Here, we are passing ArrayList to HashSet constructor to get a collection of unique elements. See the example below.

import java.util.ArrayList;
import java.util.HashSet;

public class Main {
	public static void main(String[] args){
		ArrayList<String> arrList = new ArrayList<>();
		arrList.add("Mango");
		arrList.add("Apple");
		arrList.add("Orange");
		arrList.add("Apple");
		System.out.println(arrList);
		// ArrayList to HashSet
		HashSet<String> hashSet = new HashSet<String>(arrList);
		System.out.println("HashSet:");
		System.out.println(hashSet);
	}
}


[Mango, Apple, Orange, Apple]
HashSet:
[Apple, Mango, Orange]

Example:

Let's create another example to convert ArrayList to HashSet. Here, we are using add() method to add each element one by one to HashSet and get a unique collection of elements.

public class Main {
	public static void main(String[] args){
		ArrayList<String> arrList = new ArrayList<>();
		arrList.add("Mango");
		arrList.add("Apple");
		arrList.add("Orange");
		arrList.add("Apple");
		System.out.println(arrList);
		// ArrayList to HashSet
		HashSet<String> hashSet = new HashSet<String>();
		for (String arr : arrList) {
			hashSet.add(arr);
		}
		System.out.println("HashSet:");
		System.out.println(hashSet);
	}
}


[Mango, Apple, Orange, Apple]
HashSet:
[Apple, Mango, Orange]

Example: Java 8

If you are using Java 8 or higher version then you can use the advantage of Stream API that makes our conversion code more concise and compact. Here, we are collecting ArrayList elements as HashSet using stream.

import java.util.ArrayList;
import java.util.HashSet;
import java.util.stream.Collectors;

public class Main {
	public static void main(String[] args){
		ArrayList<String> arrList = new ArrayList<>();
		arrList.add("Mango");
		arrList.add("Apple");
		arrList.add("Orange");
		arrList.add("Apple");
		System.out.println(arrList);
		// ArrayList to HashSet
		HashSet<String> hashSet = arrList.stream().collect(Collectors.toCollection(HashSet::new));
		System.out.println("HashSet:");
		System.out.println(hashSet);
	}
}


[Mango, Apple, Orange, Apple]
HashSet:
[Apple, Mango, Orange]