title: How to use HashSet to find common elements in two Comparable arrays?
body: <p><strong>EDIT</strong>: The method signature </p>
  
  <pre><code>    public Comparable[][] findCommonElements(Comparable[][] collections)
  </code></pre>
  
  <p>is wrong. It should be</p>
  
  <pre><code>    public Comparable[] findCommonElements(Comparable[][] collections)
  </code></pre>
  
  <p>but changing it in my IDE messes everything up. I almost feel like I've gone beyond my knowledge because I don't fully understand Sets, and the 2D array is messing me up badly. </p>
  
  <p>I am required to write an algorithm that takes two <strong>Comparable</strong> arrays, iterates through them with <strong>linear time efficiency</strong>, and displays the common elements. 
  I have read that using a HashSet would give me the fastest time efficiency, but I have reached an impasse. Here's why: </p>
  
  <p>We were given the instructions, and one single line of code, which is the method signature</p>
  
  <pre><code>public Comparable[][] findCommonElements(Comparable[][] collections)
  </code></pre>
  
  <p>which means I have to return the 2d array, "collections." I emailed my prof on using HashSets, and I was given the go-ahead, except I have this problem:</p>
  
  <p>"You can use HashSets inside your findCommonElements method, but you will need to be able to count the number of comparisons performed. Although hashing is usually very efficient, some comparisons will be made in the event of collisions. To do this you would need to have access to the source code for the HashSet you use. You also need a "getComparisons()" method in your CommonElements class to return the number of comparisons." </p>
  
  <p>In two semesters of programming, I did not learn HashSets, Maps, Tables, etc. I am trying to learn this myself, and I do not fully understand collisions. </p>
  
  <p>My code does take the two arrays and return the common elements, but my return statement is screwy since I basically wrote it so it would compile (the 2d Comparable array is the parameter). </p>
  
  <p>Am I on the right path with this? Here is the code: </p>
  
  <pre><code>public class CommonElements {
  
      static Comparable[] collection1 = {"A", "B", "C", "D", "E"}; //first array
      static Comparable[] collection2 = {"A", "B", "C", "D", "E", "F", "G"}; //second array
      static Comparable[][] collections = {collection1, collection2}; //array to store common elements. 
      static Set&lt;Comparable&gt; commonStuff = new HashSet&lt;&gt;(); //instance of Set containing common elements
  
      public static void main(String[] args) {
  
          CommonElements commonElements = new CommonElements(); //create instance of class CommonElements
          commonElements.findCommonElements(collections); //call the find method
  
      }
      public Comparable[][] findCommonElements(Comparable[][] collections) {
  
          Set&lt;Comparable&gt; addSet = new HashSet&lt;&gt;(); //instance of Set to add elements to
  
          for (Comparable x : collection1) { //adding elements from first array to my addSet
              addSet.add(x);
          }
          for (Comparable x : collection2) {
              if (addSet.contains(x)) {
              commonStuff.add(x); //checking for common elements, add to commonStuff Set
              }
          }
          System.out.println(toString(commonStuff)); //print the toString method
  
          return collections; //return statement, otherwise Java will whine at me
      }
      public String toString(Set&lt;Comparable&gt; commonStuff) { //this method gets rid of the brackets
          String elements = commonStuff.toString(); //make a String and assign it to the Set
          elements = elements.replaceAll("\\[", "").replaceAll("\\]", ""); //replace both brackets with empty space
  
          return "Common Elements: " + elements; //return the Set as a new String
      }
  }
  </code></pre>