title: JAVA : Set addAll() vs adding using loop
body: <p>I was trying to add Integers from 2 sets into single Set via for loop and also using addAll() method provided by Collections. For test purpose, I have populated 2 Sets with Integers and then tried adding them to third set</p>
  
  <pre><code>        Set&lt;Integer&gt;  undId = new HashSet&lt;Integer&gt;();
          Set&lt;Integer&gt;  proxies = new HashSet&lt;Integer&gt;();
          //Create 2 sets with Integers
          for(int i=0;i&lt;100;i++){
              undId.add(i);
              proxies.add(i);         
          }
  </code></pre>
  
  <p>and method 1 : //Now add them to third set using for loop</p>
  
  <pre><code>            for(Integer integer : undId)
              underlyings.add(integer);
  
          for(Integer integer :proxies)
              underlyings.add(integer);
  </code></pre>
  
  <p>and method 2 ://Or add them to third set using addAll()</p>
  
  <pre><code>            underlyings.addAll(undId);
          underlyings.addAll(proxies);    
  </code></pre>
  
  <p>Now when i was trying to time the operation using System.nanoTime(), add is twice faster (for 100,1000,10000 elements). When i increased size to 1000000 or 10000000. It was reversed. I was wondering why would it happen for larger set. I am not sure how addAll() internally handles however any help in understanding above will be appreciated. Thnx </p>