Ad

Tuesday 10 September 2013

To find the Asymmetric difference and intersection between two sets in Java using removeAll and retainAll

Set is a collection which is used to hold distinct elements i.e it doesn't allow duplicates.Set
provides two important methods retainAll and removeAll which is used to perform bulk operation like
set-algebric operations.Lets say we have two sets set1 and set2.

set1.retainall(set2)
   This will transform the set1 to hold elements which is present in both set1 and set2.Thus it will give the intersection of the two sets.

set1.removeall(set2)
   This will transform the set1 to hold elements which is present only in set1 and not in set2.Thus it will give the asymmetric difference between this two sets.  

Lets see a sample program below to understand this methods.Let us consider we have two sets,one contains the employees who have Java skill and the other contains employees who have J2ee skill.Our requirement is to:
  • Find the employees who have only Java Skill - use removeAll.
  • Find the employees who have both Java and J2ee Skill - use retainAll.
  • Find the employees who have only J2ee Skill - use removeAll.
import java.util.HashSet;
import java.util.Set;

public class SetExample {

 public static void main(String[] args) {
  Set<String> javaSkillEmpSet = getJavaSkillEmpSet();
  Set<String> j2eeSkillEmpSet = getJ2eeSkillEmpSet();
  javaSkillEmpSet.removeAll(j2eeSkillEmpSet);
  /*javaSkillEmpSet will contain employees who have only Java Skill and not having J2ee Skill*/ 
  System.out.println("Employees who have Only Java skill:"+ javaSkillEmpSet);                                                                    
  javaSkillEmpSet = getJavaSkillEmpSet();
  javaSkillEmpSet.retainAll(j2eeSkillEmpSet);
  /*javaSkillEmpSet will contain employees who have both Java  and  J2ee Skill*/ 
  System.out.println("Employees who have both Java and J2ee skill:"+ javaSkillEmpSet);

  javaSkillEmpSet = getJavaSkillEmpSet();
  j2eeSkillEmpSet.removeAll(javaSkillEmpSet);
  /*j2eeSkillEmpSet will contain employees who have only J2ee Skill and not having Java Skill*/
  System.out.println("Employees who have only J2ee skill:"+j2eeSkillEmpSet);
 }

 /**
  * This Method Will return the Set which contains employees who have Java Skill
  */
 public static Set<String> getJavaSkillEmpSet() {
  Set<String> javaSkillEmpSet = new HashSet<String>();
  javaSkillEmpSet.add("EmpId01");
  javaSkillEmpSet.add("EmpId02");
  javaSkillEmpSet.add("EmpId03");
  javaSkillEmpSet.add("EmpId04");
  return javaSkillEmpSet;
 }

 /**
  * This Method Will return the Set which contains employees who have J2ee Skill
  */
 public static Set<String> getJ2eeSkillEmpSet() {
  Set<String> j2eeSkillEmpSet = new HashSet<String>();
  j2eeSkillEmpSet.add("EmpId01");
  j2eeSkillEmpSet.add("EmpId05");
  j2eeSkillEmpSet.add("EmpId03");
  j2eeSkillEmpSet.add("EmpId06");
  return j2eeSkillEmpSet;
 }
}

Output:
Employees who have Only Java skill:[EmpId02, EmpId04]
Employees who have both Java and J2ee skill:[EmpId01, EmpId03]
Employees who have only J2ee skill:[EmpId06, EmpId05]

No comments:

Post a Comment