Check whether IdentityHashMap empty or not in Java?

Discussion RoomCategory: JavaCheck whether IdentityHashMap empty or not in Java?
Nick asked 6 years ago

Let us first create a IdentityHashMap and add some elements to it

Map<String, Integer> m= new IdentityHashMap<String, Integer>();
m.put(“1”, 100);
m.put(“2”, 200);
m.put(“3”, 300);
m.put(“4”, 150);
m.put(“5”, 110);
m.put(“6”, 50);
m.put(“7”, 90);
m.put(“8”, 250);
m.put(“9”, 350);
m.put(“10”, 450);
Now, use the following method to check whether the Map is empty or not. Since we added some elements above, therefore the Map is not empty

n.isEmpty();
Example
Live Demo

import java.util.*;
public class Demo {
public static void main(String[] args) {
Map<String, Integer> m = new IdentityHashMap<String, Integer>();
m.put(“1”, 100);
m.put(“2”, 200);
m.put(“3”, 300);
m.put(“4”, 150);
m.put(“5”, 110);
m.put(“6”, 50);
m.put(“7”, 90);
m.put(“8”, 250);
m.put(“9”, 350);
m.put(“10”, 450);
System.out.println(“Map elements = “+ m);
System.out.println(“Is the map empty? “+ m.isEmpty());
}
}
The following is the output

Map elements = {2=200, 4=150, 9=350, 7=90, 10=450, 6=50, 5=110, 8=250, 1=100, 3=300}
Is the map empty? false

Scroll to Top