How to use 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> hs = new HashSet<String>();              

        hs.add("java");
        hs.add("c");
        hs.add("c++");
        hs.add("c#");
        hs.add("python");
        hs.add("php");

        Iterator<String> itr = hs.iterator();
        while(itr.hasNext()) {
            System.out.println(itr.next());
        }

    }   
}




/*
run:

c#
c++
python
java
c
php

*/

 



answered Nov 9, 2021 by avibootz

Related questions

1 answer 265 views
265 views asked Jan 16, 2022 by avibootz
2 answers 359 views
359 views asked Feb 8, 2019 by avibootz
1 answer 254 views
2 answers 368 views
3 answers 254 views
254 views asked Jul 6, 2025 by avibootz
2 answers 255 views
1 answer 223 views
223 views asked Jul 23, 2023 by avibootz
...