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 243 views
243 views asked Nov 30, 2016 by avibootz
2 answers 258 views
1 answer 225 views
225 views asked Jul 23, 2023 by avibootz
1 answer 195 views
195 views asked Jul 22, 2023 by avibootz
1 answer 215 views
...