To convert an array of integers to a Set
in Java, you can use the Collectors.toSet()
method of the Collectors
class. This method returns a Set
that contains the elements of the array.
Here is an example of how to convert an array of integers to a Set
:
int[] arr = {1, 2, 3, 4, 5}; Set<Integer> set = Arrays.stream(arr) .boxed() .collect(Collectors.toSet()); // set is {1, 2, 3, 4, 5}
In this example, the array is first converted to a stream of integers using the Arrays.stream()
method. The boxed()
method is used to convert the stream of primitive int values to a stream of Integer
objects. Finally, the Collectors.toSet()
method is used to create a Set
that contains the elements of the array.
For more information on the Collectors.toSet()
method in Java, you can refer to the Java documentation.