title: Merge two Lists. Sens of using HashSet
body: <p>I found an algorithm containing a merge method (<a href="https://commons.apache.org/proper/commons-math/jacoco/org.apache.commons.math3.stat.clustering/DBSCANClusterer.java.html" rel="nofollow">https://commons.apache.org/proper/commons-math/jacoco/org.apache.commons.math3.stat.clustering/DBSCANClusterer.java.html</a>)</p>
  
  <pre><code>private &lt;T&gt; List&lt;T&gt; theirMerge(final List&lt;T&gt; one, final List&lt;T&gt; two) {
      final Set&lt;T&gt; oneSet = new HashSet&lt;T&gt;(one);
      for (T item : two) {
          if (!oneSet.contains(item)) {
              one.add(item);
          }
      }
      return one;
  }
  </code></pre>
  
  <p>What is the main benefit of using a <code>HashSet</code> here?</p>
  
  <p>My simpler implementation looks like this</p>
  
  <pre><code>private &lt;T&gt; List&lt;T&gt;  myMerge (ArrayList&lt;T&gt; one, ArrayList&lt;T&gt; two){
      for (T item: two) {
          if(!one.contains(item)){
              one.add(item);
          }
      }
      return one;
  }
  </code></pre>