How to use strings with HashSet in Java

2 Answers

0 votes
import java.util.HashSet;
 
public class MyClass {
    public static void main(String args[]) {
        HashSet<String> hs =new HashSet<String>();
         
        hs.add("java");
        hs.add("c");
        hs.add("php");
        hs.add("python");
        hs.add("java");

        System.out.println(hs);
    }
}
 
/*
run:

[python, java, c, php]
 
*/

 



answered Feb 8, 2019 by avibootz
0 votes
import java.util.*;
 
public class MyClass {
    public static void main(String args[]) {
        HashSet<String> hs =new HashSet<String>();
         
        hs.add("java");
        hs.add("c");
        hs.add("php");
        hs.add("python");
        hs.add("java");

        Iterator it = hs.iterator();
        while(it.hasNext()) {
            System.out.println(it.next());
        }
    }
}
 
/*
run:

python
java
c
php
 
*/

 



answered Feb 8, 2019 by avibootz

Related questions

1 answer 245 views
245 views asked Nov 30, 2016 by avibootz
2 answers 373 views
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
...