I have written a program with 2 different collections of numbers and I was wondering how would I get the union, intersection and set difference from these two collections? I know that BitSet has methods for it but those doesn't work here.
public class Collections {
public static void main(String[] args) {
// 1. Collection
Set<Integer> grp1 = new HashSet<Integer>();
grp1.add(1);
grp1.add(2);
grp1.add(3);
grp1.add(4);
grp1.add(5);
// printing 1. collection
System.out.println("1. collection: ");
Iterator<Integer> i = grp1.iterator();
while(i.hasNext()) {
int numbers1 = i.next();
System.out.print(numbers1 + " ");
}
System.out.println();
// 2. collection
Set<Integer> grp2 = new HashSet<Integer>();
grp2.add(8);
grp2.add(7);
grp2.add(6);
grp2.add(5);
grp2.add(4);
// printing 2. collection
System.out.println("2. collection: ");
Iterator<Integer> y = grp2.iterator();
while(y.hasNext()) {
int numbers2 = y.next();
System.out.print(numbers2 + " ");
// Union
// Intersection
// Difference
}
}
}