Aim:
Develop a java Programming was Collections of Set Interfaces.
Procedure:
- Step 1: Open a New Java Application on Netbeans Editor.
- Step 2: Open Netbeans Ide, Choose File new Project.
- Step 3: New Open a New Project and Choose a Java Application.
- Step 4: Create a Java File for Performing Set Operation Such as Union Intersection, Difference and Subset.
- Step 5: Run the Index Page and Verify the Result Page.
- Step 6: Save the Project (Ctrl+s).
- Step 7: Run the Project (F5).
Program:
import java.util.*; import javax.print.DocFlavor; public class sample { public static void main(String[] args) { NavigableSet<String>ss=new TreeSet<String>(); ss.add("HTML"); ss.add("CSS"); ss.add("JAVA"); NavigableSet<String>rever=ss.descendingSet(); System.out.println("normal"+ss); System.out.println("reverse"+rever); Set<String>sr=new TreeSet<String>(); sr.add("HTML"); sr.add("CSS"); sr.add("java"); System.out.print("sortedlist"+ss); Set<String>s1=new HashSet<String>(); s1.add("HTML") s1.add("CSS"); s1.add("JAVA"); Set<String>s2=new HashSet<String>(); s2.add("C++"); s2.add("BEANS"); s2.add("JAVA"); System.out.println("this is setlist"+s1); System.out.println("this is select"+s2); performUnion(s1,s2); performIntersection(s1,s2); performDifference(s1,s2); testForSubSet(s1,s2); } public static void performUnion(Set<String>s1,Set<String>s2) { Set<String>s1Unions2=new HashSet<String>(s1); s1Unions2.addAll(s2); System.out.println("union of s1 and s2"+s1Unions2); } public static void performIntersection(Set<String>s1,Set<String>s2) { Set<String>s1inters2=new HashSet<String>(s1); s1inters2.retainAll(s2); System.out.println("intersection of s1 and s2"+s1inters2); } public static void performDifference(Set<String>s1,Set<String>s2) { Set<String>s1diffs2=new HashSet<String>(s1); s1diffs2.removeAll(s2); System.out.println("Difference of s1-s2"+s1diffs2); Set<String>s2diffs1=new HashSet<String>(s2); s2diffs1.removeAll(s1); System.out.println("Difference of s2-s1"+s2diffs1); } public static void testForSubSet(Set<String>s1,Set<String>s2) { System.out.println("subset of s1 and s2"+s1.containsAll(s2)); System.out.print("subset of s1 and s2"+s2.containsAll(s1)); } }
Output:
