Saturday, January 7, 2012

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 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 sometime size of ArrayList doesn't match with size of HashSet after conversion. Converting ArrayList to Set is entirely different than Converting Map to List but has one thing common , A constructor which takes a collection object. HashSet also has a constructor which takes another collection object e.g. ArrayList and creates Set out of those element. we have also seen a bit of this on 10 Examples of ArrayList in Java and we will see here in detail.

How to Convert List to Set 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();
        companies.add("Sony");
        companies.add("Samsung");
        companies.add("Microsoft");
        companies.add("Intel");
        companies.add("Sony");
      
        System.out.println("Size of ArrayList before Converstion: " + companies.size());
        System.out.println(companies);
      
        //Converting ArrayList into HashSet in Java
        HashSet companySet = new HashSet(companies);
      
        System.out.println("Size of HashSet after Converstion: " + companies.size());
        System.out.println(companySet);
  
    }
}

Output:
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.

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

5 comments :

Anonymous said...

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

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.

Post a Comment