title: Why is creating a HashMap faster than creating an Object[]?
body: <p>I've tried to build my own Map to increase the performance for a special environment, and I realized something pretty interesting: Creating a <code>new Hashmap&lt;Integer,String&gt;(2000)</code> is faster than <code>new Object[2000]</code> - no matter in which order I execute these commands. That's pretty confusing to me, esp. because the Hashmap constructor contains a <code>table = new Entry[capacity]</code>, according to <a href="http://www.docjar.com/html/api/java/util/HashMap.java.html" rel="nofollow noreferrer" title="source code">this</a>. Is there something wrong with my testbench?</p>
  
  <pre><code>public static void test(int amm){ //amm=1_000_000
      Map&lt;Integer,String&gt; m1 = null;
      Object[] arr = null;
  
      long time = System.nanoTime();
      for(int i = 0; i &lt; amm; i++){
          m1 = new HashMap&lt;Integer, String&gt;(2000);
      }
      System.out.println("m1: " + (System.nanoTime() - time)); //m1: 70_455_065
  
      time = System.nanoTime();
      for(int i = 0; i &lt; amm; i++){
          arr = new Object[2000];
      }
      System.out.println("arr: " + (System.nanoTime() - time)); //arr: 1_322_473_803
  }
  </code></pre>
  
  <p>I'd love to see the results of testing on another computer. I've got no clue why creating a <code>HashMap</code> is 10 times faster than creating a <code>Object[]</code>.</p>