title: Cartesian Products with sets in java
body: <p>I am trying to create a cartesian product method in java that accepts sets as arguments and returns a set pair. The code I have coverts the argumented sets to arrays and then does the cartesian product but i can't add it back to the set pair that i want to return. Is there an easier way to do this? Thanks in advance.</p>
  
  <pre><code>public static &lt;S, T&gt; Set&lt;Pair&lt;S, T&gt;&gt; cartesianProduct(Set&lt;S&gt; a, Set&lt;T&gt; b) {
      Set&lt;Pair&lt;S, T&gt;&gt; product = new HashSet&lt;Pair&lt;S, T&gt;&gt;();
  
  
      String[] arrayA = new String[100];
      String[] arrayB= new String[100];
  
      a.toArray(arrayA);
      b.toArray(arrayB);
  
      for(int i = 0; i &lt; a.size(); i++){
          for(int j = 0; j &lt; b.size(); j++){
              product.add(arrayA[i],arrayB[j]);
          }
      }
      return product;
  }
  </code></pre>