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 361 views
361 views asked Feb 8, 2019 by avibootz
1 answer 268 views
268 views asked Jan 16, 2022 by avibootz
1 answer 207 views
207 views asked Nov 9, 2021 by avibootz
1 answer 256 views
3 answers 255 views
255 views asked Jul 6, 2025 by avibootz
2 answers 259 views
...