How to initialize a set with numbers in Java

2 Answers

0 votes
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class MyClass {
    public static void main(String args[]) {
        Set<Integer> set = new HashSet<>();
        
        Collections.addAll(set, 1, 7, 0, 9, 3, 5, 8, 6, 99, 1200);
        
        System.out.println(set);
    }
}




/*
run:

[0, 1200, 1, 3, 99, 5, 6, 7, 8, 9]

*/

 



answered Jul 17, 2022 by avibootz
0 votes
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class MyClass {
    public static void main(String args[]) {
        Set<Integer> set = new HashSet<>(Arrays.asList(1, 7, 0, 9, 3, 5, 8, 6, 99, 1200));

        System.out.println(set);
    }
}




/*
run:

[0, 1200, 1, 3, 99, 5, 6, 7, 8, 9]

*/

 



answered Jul 17, 2022 by avibootz

Related questions

2 answers 166 views
2 answers 146 views
1 answer 152 views
1 answer 160 views
1 answer 160 views
...