title: How does Java quickly compare two collection are exactly the same in Java?
body: <p>How does Java quickly compare two collection are exactly the same in java? </p>
  
  <p>for example:</p>
  
  <p>if this collection is Set, compare their contain the same object and their size are same.</p>
  
  <p>Code looks like the following</p>
  
  <pre><code>public boolean isSameSets(Set&lt;T&gt; set1, Set&lt;T&gt; set2){
  
      if (set1.size() != set2.size()) {
          return false;
      }
  
      return set1.containAll(set2) &amp;&amp; set2.containAll(set1);
  }
  </code></pre>
  
  <p>But if the collection is list, because list is sorted collection, we can iterate to compare every element, I think this way is not the best, who could please tell me how to quickly compare them? </p>
  
  <p>========================================================</p>
  
  <p>Thanks everyone, actually, Collection equals method can do it, include Set and List.</p>