title: Usage of Java 9 collection factories
body: <p>In the context of the comments and answers given at  <a href="https://stackoverflow.com/questions/39400238">List.of() or Collections.emptyList()</a> and <a href="https://stackoverflow.com/questions/39385658">List.of(...) or Collections.unmodifiableList()</a> I came up with two following rules of thumb (which also apply to <code>Set</code> and <code>Map</code> factories accordingly).</p>
  
  <ol>
  <li>Don't replace all occurrences</li>
  </ol>
  
  <p>Keep using <code>Collections.emptyList()</code> for readability and when e.g. initializing lazy field members like:</p>
  
  <pre><code>class Bean {
    private List&lt;Bean&gt; beans = Collection.emptyList();
    public List&lt;Bean&gt; getBeans() {
      if (beans == Collections.EMPTY_LIST) { beans = new ArrayList&lt;&gt;(); }
      return beans;
    }
  }
  </code></pre>
  
  <ol start="2">
  <li>Use new factories as method argument builders</li>
  </ol>
  
  <p>Use new factories <code>List.of()</code> and variants as quick and less-to-type version, when calling an executable with <code>List</code> parameter(s). Here are my current substitution patterns:</p>
  
  <pre><code>Collections.emptyList()       --&gt; List.of()
  Collections.singletonList(a)  --&gt; List.of(a)
  Arrays.asList(a, ..., z)      --&gt; List.of(a, ..., z)
  </code></pre>
  
  <p>In a fictional usage of <code>Collections.indexOfSubList</code>, the following lines</p>
  
  <pre><code>Collections.indexOfSubList(Arrays.asList(1, 2, 3), Collections.emptyList());
  Collections.indexOfSubList(Arrays.asList(1, 2, 3), Collections.singletonList(1));
  Collections.indexOfSubList(Arrays.asList(1, 2, 3), Arrays.asList(1));
  Collections.indexOfSubList(Arrays.asList(1, 2, 3), Arrays.asList(2, 3));
  Collections.indexOfSubList(Arrays.asList(1, 2, 3), Arrays.asList(1, 2, 3));
  </code></pre>
  
  <p>will read</p>
  
  <pre><code>Collections.indexOfSubList(List.of(1, 2, 3), List.of());
  Collections.indexOfSubList(List.of(1, 2, 3), List.of(1));
  Collections.indexOfSubList(List.of(1, 2, 3), List.of(1));
  Collections.indexOfSubList(List.of(1, 2, 3), List.of(2, 3));
  Collections.indexOfSubList(List.of(1, 2, 3), List.of(1, 2, 3));
  </code></pre>
  
  <p>Do you (dis-)agree?</p>