Convert Arrays to Set in Java

java-logo

Java 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));
Code language: Java (java)

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);
Code language: Java (java)

Output:

[Italy, Switzerland, India]
Code language: Java (java)

Hope this is useful.

Get our Articles via Email. Enter your email address.

You may also like...

9 Comments

  1. Very good tutorial

    • Mohan says:

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

  2. Java Expert says:

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

  3. andynaz says:

    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));

  4. Ank says:

    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!

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

  5. Bikram says:

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

  6. Rio says:

    My Code for int[]:

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

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

  7. Tami says:

    Hi,
    But to convert an array to set , why the overhead of creating the mediator “List”.

    Is there any other more efficient method ?

Leave a Reply

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