title: What is the fastest way to fill an ArrayList with null in java?
body: <p>I want a List of n Sets of Integers and initially this list should be filled with null.
  A lot of the Sets will be initialised later, and some will remain null.</p>
  <p>I have tried different methods to implement this, some of them are included here:</p>
  <pre><code>List&lt;HashSet&lt;Integer&gt;&gt; List_of_Sets = Arrays.asList(new HashSet[n]);
  </code></pre>
  <pre><code>ArrayList&lt;HashSet&lt;Integer&gt;&gt; List_of_Sets = new ArrayList&lt;&gt;(n);
  while(n-- &gt; 0) List_of_Sets.add(null);
  </code></pre>
  <p><strong>Is there a faster way to do this?</strong></p>
  <p><em>For clarification</em> an example for arrays would be <code>Arrays.fill()</code> used to be slower than:</p>
  <pre><code>/*
   * initialize a smaller piece of the array and use the System.arraycopy 
   * call to fill in the rest of the array in an expanding binary fashion
   */
  public static void bytefill(byte[] array, byte value) {
    int len = array.length;
  
    if (len &gt; 0){
      array[0] = value;
    }
  
    //Value of i will be [1, 2, 4, 8, 16, 32, ..., len]
    for (int i = 1; i &lt; len; i += i) {
      System.arraycopy(array, 0, array, i, ((len - i) &lt; i) ? (len - i) : i);
    }
  }
  </code></pre>
  <p>^above code is from Ross Drew's answer to <a href="https://stackoverflow.com/questions/9128737/fastest-way-to-set-all-values-of-an-array">Fastest way to set all values of an array?</a></p>