How to create HashSet in Java

1 Answer

0 votes
import java.util.HashSet;
import java.util.Iterator;

public class MyClass {
    public static void main(String args[]) {
        HashSet<String> hset = new HashSet<>();
         
        hset.add("java");
        hset.add("c");
        hset.add("c++");
        hset.add("c#");
        hset.add("python");
        
        System.out.println(hset);
           
        Iterator<String> s = hset.iterator();  
        while(s.hasNext()) {  
           System.out.println(s.next());  
        }
    }
}



 
/*
run:
    
[c#, c++, python, java, c]
c#
c++
python
java
c
  
*/

 



answered Jan 16, 2022 by avibootz
edited Jul 6, 2025 by avibootz

Related questions

3 answers 254 views
254 views asked Jul 6, 2025 by avibootz
1 answer 256 views
1 answer 241 views
241 views asked Nov 30, 2016 by avibootz
2 answers 255 views
1 answer 222 views
222 views asked Jul 23, 2023 by avibootz
1 answer 192 views
192 views asked Jul 22, 2023 by avibootz
1 answer 212 views
...