How to initialize a set with characters 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<Character> set = new HashSet<>();
        
        Collections.addAll(set, 'v', 'p', 'w', 'e', 'r', 'z', 'x');
        
        System.out.println(set);
    }
}




/*
run:

[p, r, e, v, w, x, z]

*/

 



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<Character> set = new HashSet<>(Arrays.asList('v', 'p', 'w', 'e', 'r', 'z', 'x'));

        System.out.println(set);
    }
}




/*
run:

[p, r, e, v, w, x, z]

*/

 



answered Jul 17, 2022 by avibootz

Related questions

2 answers 168 views
2 answers 130 views
1 answer 156 views
1 answer 162 views
1 answer 142 views
...