Complete this method in Java Remove from this set any of its
Complete this method in Java.
Remove from this set any of its elements that are not contained in another set.
* The result is this set intersected with the other set
* @param set2 * a set whose elements will be intersected with this set
* @precondition * The parameter, set2, is not null.
* @postcondition * This set contains the intersection of itself with set2.
public void keepCommonElements(IntSet set2) {
int [] arr = set2.toArray();
}
Solution
public void keepCommonElements(HashSet<Integer> set2)
{
int count;
if(this!=null && set2!=null)
{
int[] arr=set2.toArray();
int[] arr1=this.toArray();
for(int i=0;i<arr.length;i++)
{
count=0;
for(int j=0;j<arr1.length;j++)
{
if(arr[i]==arr1[j])
{
count++;
}
}
if(count==0)
{
this.remove(arr[i]);
}
}
}
else System.out.println(\" null set\");
}

