The program begins with the import statement import java.util.*; , which imports all classes from the java.util package, including HashSet.
Next, the program defines a public class called RemoveFirst_Occurrence. Within the RemoveFirst_Occurrence class, the main method is declared and serves as the entry point for the program. It takes an array of strings (args) as input.
Inside the main method, a new instance of the HashSet class is created using the constructor: HashSet
After that, the program adds several elements to the HashSet using the add() method, similar to the previous program. The elements added are "C", "JAVA", "CPP", "PYTHON", "MYSQL", and "RUBY". Next, the program prints the contents of the HashSet using the System.out.println() statement. It displays the message "Given HashSet: " followed by the elements contained in the HashSet.
The program then removes two elements from the HashSet using the remove() method. The elements being removed are "MYSQL" and "CPP". Finally, the program prints the updated contents of the HashSet using another System.out.println() statement. It displays the message "After Removing in HashSet: " followed by the remaining elements in the HashSet.
import java.util.*; public class RemoveFirst_Occurrence { public static void main(String args[]) { 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); //remove "MYSQL","CPP" h_set.remove("MYSQL"); h_set.remove("CPP"); System.out.println("After Removing in HashSet : " +h_set); } }
Given HashSet : [JAVA, CPP, C, MYSQL, RUBY, PYTHON] After Removing in HashSet : [JAVA, C, RUBY, PYTHON]
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions