title: Groovy: Difference with a.intersect( b ) and b.intersect( a )
body: <p>Why in Groovy, when I create 2 lists, is there a difference if I do a.intersect( b ) and b.intersect( a ):</p>
  
  <pre><code>def list1 = ["hello", "world", "world"];
  def list2 = ["world", "world", "world"];
  
  println( "Intersect list1 with list2: " + list1.intersect( list2 ) );
  println( "Intersect list2 with list1: " + list2.intersect( list1) );
  </code></pre>
  
  <p>traces:</p>
  
  <pre><code>Intersect list1 with list2: [world, world, world]
  Intersect list2 with list1: [world, world]
  </code></pre>
  
  <p>(you can copy it here: <a href="http://groovyconsole.appspot.com/" rel="noreferrer">http://groovyconsole.appspot.com/</a> if you want to test it)</p>
  
  <p>If the arrays all contain unique elements, then it works as normal. Once you begin to add duplicates, it gets weird:</p>
  
  <pre><code>def list1 = ["hello", "world", "test", "test"];
  def list2 = ["world", "world", "world", "test"];
  
  println( "Intersect list1 with list2: " + list1.intersect( list2 ) );
  println( "Intersect list2 with list1: " + list2.intersect( list1 ) );
  </code></pre>
  
  <p>traces:</p>
  
  <pre><code>Intersect list1 with list2: [world, world, world, test]
  Intersect list2 with list1: [world, test, test]
  </code></pre>
  
  <p>I thought the whole point of <code>intersect()</code> was to give you the common elements, so it didn't matter which order you put them in? </p>
  
  <p>If this isn't the case, how can I get only the common elements (expect duplicates in the array). E.g. example one should return <code>["world", "world"]</code> and example two should return <code>["world", "test"]</code></p>
  
  <p><strong>Edit</strong></p>
  
  <p>To clarify a bit, this code should test that user data is still the same (assuming they disconnected in the middle of something and we want to make sure the data hasn't been tampered with, or is in the same state as before).</p>
  
  <p>The order of the lists can't be guaranteed (the user could reorder it, but it's still technically the "same"), and duplicates are possible.</p>
  
  <p>So something like: <code>["one", "one", "two"]</code> should match <code>["two", "one", "one"]</code>, whereas any addition to the lists, or change in data shouldn't match.</p>