How to check if HashMap is empty in Java

1 Answer

0 votes
import java.util.HashMap;
 
public class MyClass {
    public static void main(String args[]) {
        HashMap<String, Integer> hashmap = new HashMap<>();
 
        if (hashmap.isEmpty()) {
            System.out.println("empty");
        }
        
        hashmap.put("java", 1);
        hashmap.put("c", 2);
        hashmap.put("c++", 3);
        hashmap.put("python", 4);
        hashmap.put("swift", 5);
        
        if (!hashmap.isEmpty()) {
            System.out.println("not empty");
        }
    }
}
 
 
 
 
/*
run:
 
empty
not empty
 
*/

 

 



answered Oct 10, 2020 by avibootz
edited Oct 10, 2020 by avibootz

Related questions

1 answer 160 views
1 answer 211 views
2 answers 272 views
1 answer 242 views
2 answers 225 views
1 answer 232 views
...