Tuesday, August 18, 2020

Java Program to Perform Set Operations

Set in java-

       The set interface is  present in the java.util  package. It extends the Collection interface.

       It is an unordered collection of objects in which duplicate values are not allowed.

        It is an interface which can implements the mathematical set.

       This interface contains the methods inherited from the Collection interface and adds a feature to restricts the insertion of the duplicate elements.

       The set interface allows the users to perform the basic mathematical set operations as following.

        Intersection: This operation returns all the common elements from the given two sets.

       Union: This operation adds all the elements in one set with the other. 

       Difference: This operation removes all the values present in one set from the other set.

        --------------------------------------------------Program------------------------------------------------------

/**
 *
 * @author Shikha
 */
import java.util.*; 
public class SetExample 
public static void main(String args[]) 
{       int ans=0;
                char ch='n';
Set<String> a = new HashSet<String>(); 
                a.add("Mumbai");
                a.add("Delhi");
                a.add("Chennai");
                a.add("Calcutta");
                a.add("Bengaluru");    
                a.add("Hyderabad");
                Set<String> b = new HashSet<String>(); 
                b.add("Mumbai");
                b.add("Delhi");
                b.add("Chennai");
                b.add("Calcutta");
                b.add("Bengaluru");  
b.add("Pune");  
                b.add("Indore");  
               
                          
               Scanner sc=new Scanner(System.in);
               //System.out.print(" 1:Union\t 2:Insertion\t 3:Difference\t 4:Stop");
                do{
                System.out.println(" 1:Union\t 2:Insertion\t 3:Difference\t 4:Stop");    
                ans=sc.nextInt();   
                    
                switch(ans)
                {
                
                    case 1:
                    Set<String> union = new HashSet<String>(a); 
                    union.addAll(b); 
                    System.out.println("Union of the two Set"); 
                    System.out.println(union);
                    break;
                    
                    case 2:
                    Set<String> intersection = new HashSet<String>(a); 
                    intersection.retainAll(b); 
                    System.out.println("Intersection of the two Set"); 
                    System.out.println(intersection);       
                    break;
                    
                    
                    case 3:
                    Set<String> difference = new HashSet<String>(a); 
                    difference.removeAll(b); 
                    System.out.println("Difference of the two Set"); 
                    System.out.println(difference); 
                    break;
                    
                    default:
                    System.out.println("Wrong Choice");   
                    
                            
                }
                
                System.out.println("Do you want to continue Y/N");
                ch=sc.next().charAt(0); 
                }while((ch=='y')||(ch=='Y'));

------------------------------------------------------------------------------------------------------------------------
OUTPUT
 1:Union 2:Insertion 3:Difference 4:Stop
1
Union of the two Set
[Delhi, Calcutta, Chennai, Bengaluru, Pune, Indore, Mumbai, Hyderabad]
Do you want to continue Y/N
y
 1:Union 2:Insertion 3:Difference 4:Stop
2
Intersection of the two Set
[Delhi, Calcutta, Chennai, Bengaluru, Mumbai]
Do you want to continue Y/N
y
 1:Union 2:Insertion 3:Difference 4:Stop
3
Difference of the two Set
[Hyderabad]
Do you want to continue Y/N
n

No comments:

Post a Comment

Java Program to Perform Set Operations

Set in java- •        The set interface is   pres ent in the java.util   package. It extends the Collection interface. •        It  is an ...