title: Java collection methods for union, Intersection and set difference
body: <p>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. </p>
  
  <pre><code>public class Collections {
  
  public static void main(String[] args) {
  
      // 1. Collection
      Set&lt;Integer&gt; grp1 = new HashSet&lt;Integer&gt;();
  
      grp1.add(1);
      grp1.add(2);
      grp1.add(3);
      grp1.add(4);
      grp1.add(5);
  
      // printing 1. collection
      System.out.println("1. collection: ");
      Iterator&lt;Integer&gt; i = grp1.iterator();
      while(i.hasNext()) {
          int numbers1 = i.next();
          System.out.print(numbers1 + " ");
      }
      System.out.println();   
  
      // 2. collection
      Set&lt;Integer&gt; grp2 = new HashSet&lt;Integer&gt;();
  
      grp2.add(8);
      grp2.add(7);
      grp2.add(6);
      grp2.add(5);
      grp2.add(4);
  
      // printing 2. collection
      System.out.println("2. collection: ");
      Iterator&lt;Integer&gt; y = grp2.iterator();
      while(y.hasNext()) {
          int numbers2 = y.next();
          System.out.print(numbers2 + " ");
  
      // Union
  
  
      // Intersection
  
  
      // Difference
  
           }
       }
  
  }
  </code></pre>