How to use HashSet with characters in Java

2 Answers

0 votes
import java.util.HashSet;

public class MyClass {
    public static void main(String args[]) {
        HashSet<Character> hs =new HashSet<Character>();
        
        hs.add('j');
        hs.add('a');
        hs.add('v');
        hs.add('a');
        
        System.out.println(hs);
    }
}

/*
run:

[a, v, j]

*/

 



answered Feb 8, 2019 by avibootz
0 votes
import java.util.*;

public class MyClass {
    public static void main(String args[]) {
        HashSet<Character> hs =new HashSet<Character>();
        
        hs.add('j');
        hs.add('a');
        hs.add('v');
        hs.add('a');
        
        Iterator it = hs.iterator();
        while(it.hasNext()) {
            System.out.println(it.next());
        }
    }
}


/*
run:

a
v
j

*/

 



answered Feb 8, 2019 by avibootz

Related questions

2 answers 359 views
359 views asked Feb 8, 2019 by avibootz
1 answer 265 views
265 views asked Jan 16, 2022 by avibootz
1 answer 204 views
204 views asked Nov 9, 2021 by avibootz
1 answer 254 views
3 answers 254 views
254 views asked Jul 6, 2025 by avibootz
2 answers 255 views
...