How to convert an Array to a Set in Java

To convert an array to a Set in Java, you can use the Arrays.asList() method to create a List from the array, and then use the List.toSet() method to create a Set from the List.

Here is an example of how to convert an array to a Set:

import java.util.Arrays;
import java.util.List;
import java.util.Set;

public class Main {
  public static void main(String[] args) {
    String[] array = {"a", "b", "c"};
    List<String> list = Arrays.asList(array);
    Set<String> set = new HashSet<>(list);
    System.out.println(set);  // Outputs "[a, b, c]"
  }
}

This code creates an array of strings and converts it to a List using the Arrays.asList() method. It then creates a Set from the List using the HashSet constructor that takes a Collection as an argument.

You can use a different Set implementation, such as TreeSet, to specify a custom ordering for the elements in the Set. For example:

Set<String> set = new TreeSet<>(list);

This code creates a TreeSet from the List, which will maintain the elements in ascending order according