How to initialize set from array elements in Java

1 Answer

0 votes
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

public class MyClass {
    public static void main(String args[]) {
        int[] arr = { 1, 5, 5, 5, 1, 2, 7, 4, 4, 4, 4, 9, 12, 16, 12, 16 };
        
        Set<Integer> st = Arrays.stream(arr).boxed().collect(Collectors.toSet());
 
        System.out.println(st);
    }
}




/*
run:

[16, 1, 2, 4, 5, 7, 9, 12]

*/

 



answered Nov 21, 2021 by avibootz

Related questions

1 answer 152 views
2 answers 166 views
2 answers 146 views
2 answers 126 views
1 answer 131 views
...