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 121 views
121 views asked Jul 6, 2025 by avibootz
1 answer 176 views
1 answer 173 views
173 views asked Nov 30, 2016 by avibootz
2 answers 147 views
1 answer 134 views
134 views asked Jul 23, 2023 by avibootz
1 answer 113 views
113 views asked Jul 22, 2023 by avibootz
1 answer 132 views
...