Java Collection How to - Convert Array to a Set








Question

We would like to know how to convert Array to a Set.

Answer

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
//from w w  w  . j  av a 2  s .c o m
public class MainClass {

  public static void main(String[] a) {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set set = new HashSet(Arrays.asList(elements));

    System.out.println(set);

  }

}

The code above generates the following result.