In the program, two HashSet objects, h_set1 and h_set, are created. h_set is populated with some elements: "C", "JAVA", "CPP", "PYTHON", "MYSQL", and "RUBY". The program then checks if h_set1 and h_set are empty using the isEmpty() method. The isEmpty() method returns true if the HashSet is empty, and false otherwise.
Finally, the program removes all elements from h_set using the removeAll() method, passing h_set as an argument. This method removes all elements from the HashSet, effectively making it empty. The contents of the HashSet are printed before and after the removal operation.
The output shows the initial contents of h_set, followed by whether h_set1 and h_set are empty or not. Then, after removing all elements from h_set, the program prints the contents of h_set, which is now an empty HashSet.
import java.util.*; public class HashSet_EmptyNot { public static void main(String[] args) { HashSet<String> h_set1 = new HashSet<String>(); HashSet<String> h_set = new HashSet<String>(); h_set.add("C"); h_set.add("JAVA"); h_set.add("CPP"); h_set.add("PYTHON"); h_set.add("MYSQL"); h_set.add("RUBY"); System.out.println("Given HashSet : " + h_set); System.out.println("Check HashSet 1 is Empty or Not : "+h_set1.isEmpty()); System.out.println("Check HashSet 2 is Empty or Not : "+h_set.isEmpty()); System.out.println("Remove all the elements from a HashSet .. "); h_set.removeAll(h_set); System.out.println("After Removing all the Elements in HashSet : "+h_set); } }
Given HashSet : [JAVA, CPP, C, MYSQL, RUBY, PYTHON] Check HashSet 1 is Empty or Not : true Check HashSet 2 is Empty or Not : false Remove all the elements from a HashSet .. After Removing all the Elements in HashSet : []
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions