How to convert ArrayList to Set in Java with Example

Converting ArrayList to Set in Java means creating a Set implementation like HashSet from an ArrayList full of objects. Before Converting your ArrayList into HashSet do remember that List keep insertion order and guarantees the same but Set doesn't have such obligation. Also, List allows duplicates but Set doesn't allow any duplicates, which means if you have duplicates in your ArrayList they will be lost when you convert ArrayList to HashSet and that's the reason why the sometime size of ArrayList doesn't match with the size of HashSet after conversion.

Converting ArrayList to Set is entirely different than Converting Map to List but has one thing common, A constructor that takes a collection object. HashSet also has a constructor that takes another collection object e.g. ArrayList and creates Set out of those elements. 

We have also seen a bit of this on 10 Examples of ArrayList in Java and we will see here in detail.

And, If you are new to Java world then I also recommend you go through The Complete Java MasterClass on Udemy to learn Java in a better and more structured way. This is one of the best and up-to-date courses to learn Java online.

How to Convert List to Set in Java

Now, let's  see a couple of ways to convert a List to st in Java :

ArrayList to Set Conversion Example
How to convert List to Set in Java ExampleConverting ArrayList or any other List implementation into HashSet or any other Set Implementation is not very difficult to write. In this Java tutorial  we will see complete code example of converting ArrayList to HashSet in Java

package test;

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

public class ArrayListToSetConverter {

    public static void main(String args[]){
        //Creating ArrayList for converting into HashSet
        ArrayList companies = new ArrayList();
        System.out.println("Size of ArrayList before Converstion: " + companies.size());
        //Converting ArrayList into HashSet in Java
        HashSet companySet = new HashSet(companies);
        System.out.println("Size of HashSet after Converstion: " + companies.size());

Size of ArrayList before Converstion: 5
[Sony, Samsung, Microsoft, Intel, Sony]
Size of HashSet after Converstion: 5
[Sony, Microsoft, Intel, Samsung]

You might have noticed that Size of Converted ArrayList cum HashSet is not same and one less than original  ArrayList because duplicate entry "Sony" is just one time in Set. This is another great way of removing  duplicates from ArrayList. just copy entries of ArrayList into Set and than copy it back into ArrayList you  don't have duplicates anymore.

Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures and Algorithms: Deep Dive Using Java

That’s all on quick tip to Convert an ArrayList into HashSet in Java. You may check difference between ArrayList and Vector to know more about ArrayList and other Collection class.

Related Java Tutorial


Anonymous said...

Size of HashSet after Converstion should be 4 if i am not mistaken.please correct me if i am wrong.

Javin @ hashtable example java said...

@Anonymous you are absolutely correct, Size must be four its a bug if you see carefully I am still printing size of list instead of size of hashset in Java.
companies.size() //size of list.
will correct it. thanks for pointing it out.

Bharti said...

ArrayList to HashSet conversion is my most preferred method to remove duplicates from collection if Insertion order is not concern, as insertion order gets lost when you copy elements form ArrayList to HashSet. if you still wants to use HashSet to remove duplicates and want to keep the insertion order of List than consider using LinkedHashSet.

Anonymous said...

It's 4 if he printing companySet.size(). However, I think there is a typo still printing companies.size() which is 5

Pawankumar said...

Adding more - Its always a great idea to check the list for null(if list is returned by a method or list items are supplied from another object), else you will encounter NPE while creating the HashSet instance.

Anonymous said...

how to implement set using list ? Set shouldn't be used.

Post a Comment