How to create a HashSet of strings in Java

1 Answer

0 votes
package javaapplication1;

import java.util.HashSet;

public class JavaApplication1 {

    public static void main(String[] args) {

        try {

            HashSet<String> hash = new HashSet<>();
            hash.add("java");
            hash.add("c");
            hash.add("c++"); 
            hash.add("c++");
            hash.add("php");

            System.out.println("size: " + hash.size());

            if (hash.contains("java"))
                System.out.println("yes");
            else
                System.out.println("no");
            
            if (hash.contains("c++"))
                System.out.println("yes");
            else
                System.out.println("no");
            
            if (hash.contains("python"))
                System.out.println("yes");
            else
                System.out.println("no");

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
             
run:

size: 4
yes
yes
no
    
 */

 



answered Nov 30, 2016 by avibootz

Related questions

2 answers 358 views
358 views asked Feb 8, 2019 by avibootz
1 answer 186 views
186 views asked Jan 16, 2022 by avibootz
3 answers 254 views
254 views asked Jul 6, 2025 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
...