How to check if a HashSet is empty 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.isEmpty())
                System.out.println("yes");
            else
                System.out.println("no");
     
        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
             
run:

size: 4
no
    
 */

 



answered Nov 30, 2016 by avibootz

Related questions

1 answer 141 views
141 views asked Feb 16, 2021 by avibootz
1 answer 183 views
1 answer 212 views
1 answer 231 views
2 answers 196 views
1 answer 206 views
...