The code you provided demonstrates the usage of HashSet in Java to remove elements from one set that are present in another set. Here's a breakdown of how the code works:
If you run this code, it will output the results of the removal operations and display the remaining elements in n1, n2, and n3.
import java.util.*; public class Remove_OtherHashSet { public static void main(String[] args) { HashSet <Integer> n1 = new HashSet<Integer>(); n1.add(10); n1.add(20); n1.add(30); n1.add(40); n1.add(50); n1.add(60); HashSet < Integer > n2 = new HashSet<Integer>(); n2.add(10); n2.add(20); n2.add(30); n2.add(40); HashSet < Integer > n3 = new HashSet<Integer>(); n3.add(100); n3.add(200); n3.add(300); n3.add(400); if (n1.removeAll(n2)) { System.out.println("Removed Items of HashSet2 contained in HashSet1."); } else { System.out.println("Unable to Remove Items from HashSet1."); } if (n1.removeAll(n3)) { System.out.println("Removed Items of HashSet3 contained in HashSet1."); } else { System.out.println("Unable to Remove Items from HashSet1."); } System.out.println("HashSet 1 : " + n1); System.out.println("HashSet 2 : " + n2); System.out.println("HashSet 3 : " + n3); } }
Removed Items of HashSet2 contained in HashSet1. Unable to Remove Items from HashSet1. HashSet 1 : [50, 60] HashSet 2 : [20, 40, 10, 30] HashSet 3 : [400, 100, 200, 300]
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions