Convert Arrays to Set in Java

java-logoJava Collection API is one of the most useful APIs used in any Java application. In my day to day Java coding routine, I have to deal with these APIs quite often.

However sometime while working with Collection API, lot of developers end up writing unnecessary and mostly inefficient code. For example, to convert an Java Array to ArrayList, I have seen people writing loops instead of simple Arrays.asList().

Here is a simple writeup on Converting Java Arrays to ArrayList that I wrote a while ago.

One of such simple requirement is to convert Java Arrays to Set. While working with Hibernate, I once had to convert a Java Arrays that we used to populate from UI and convert it into Set. While this is a simple task, most often one may end up writing for loop.

Here is a dead simple trick. Use below code to Convert Arrays to Set in Java.

Set<T> mySet = new HashSet<T>(Arrays.asList(someArray));

Tanaa!!! Simple isn’t it. Its like “I already knew that” stuff.

Notice how we have used Generics also in above code snippet. Thus if you have an ArrayList than you can convert it to Set with one simple line of code.

Checkout below example:

Example: Java Array to Set

String [] countries = {"India", "Switzerland", "Italy"}; 
		
Set<String> set = new HashSet<String>(Arrays.asList(countries));
System.out.println(set);

Output:

[Italy, Switzerland, India]

Hope this is useful.



8 Comments

  • Cloud Computing 26 March, 2012, 22:55

    Very good tutorial

    • Mohan 1 June, 2012, 14:56

      Indeed nice tip. Its kind of shortcut to me for going from array to collections directly. few more tips on arraylist here

  • Java Expert 19 April, 2012, 16:52

    I think with such effective snippets one can become more productive at work. Thanks Anyway :)

  • andynaz 23 October, 2012, 15:41

    Studying for the SCJP exam, I found out that trick, but I have a really simple doubt: how do I convert an int[] to a Set?

    Today I did it by passing through a new array Integer[] using the System.arraycopy method:

    int[] intArray = {1,2,3,4,5};
    Integer[] integerArray = new Integer[intArray.length];
    System.arraycopy(intArray, 0, integerArray, 0, intArray.length);
    Set integerSet = new HashSet(Arrays.asList(integerArray));

  • Ank 9 May, 2013, 16:28

    Sorry for nitpicking but you got the spelling of countries (countires) wrong there. Luckily you have used the same wrong spelling both times!

    Thanks for sharing this tip, though!

    • Viral Patel 9 May, 2013, 16:37

      haha.. never noticed that.. thanks, I fixed it..

  • Bikram 31 July, 2013, 12:29

    Collections.addAll(set, array) is more efficient.
    http://docs.oracle.com/javase/6/docs/api/java/util/Collection.html#addAll(java.util.Collection)

  • Rio 16 September, 2014, 17:51

    My Code for int[]:

    Integer[] arr=new Integer[]{1,2,3};

    List arr1=Arrays.asList(arr);
    System.out.println(arr1);

Leave a Reply

Your email address will not be published. Required fields are marked *

Note

To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]

Current ye@r *