title: Guava ImmutableMap Builder syntax
body: <p>I've been using Guava's <code>ImmutableMap</code> with a <code>Builder</code> for awhile without giving much thought to how/why it works. I've been using the builder in the way described by the <a href="https://google.github.io/guava/releases/19.0/api/docs/com/google/common/collect/ImmutableMap.Builder.html" rel="noreferrer">Javadoc</a>:</p>
  
  <pre><code>ImmutableMap&lt;String, Integer&gt; WORD_TO_INT =
            new ImmutableMap.Builder&lt;String, Integer&gt;()
                .put("one", 1)
                .put("two", 2)
                .put("three", 3)
                .build();
  </code></pre>
  
  <p>I've also seen examples <a href="https://stackoverflow.com/a/9489463/772385">elsewhere</a> on SO of this syntax:</p>
  
  <pre><code>ImmutableMap&lt;String,Integer&gt; myMap = ImmutableMap.&lt;String, Integer&gt;builder()
      .put("one", 1) 
      .put("two", 2) 
      .put("three", 3) 
      .build();
  </code></pre>
  
  <p>Both seem to produce the exact same result.</p>
  
  <p>A quick glance at the <code>ImmutableMap</code> <a href="https://github.com/google/guava/blob/master/guava/src/com/google/common/collect/ImmutableMap.java" rel="noreferrer">source</a> shows that the static <code>builder()</code> call in the 2nd example returns: <code>new Builder&lt;K, V&gt;()</code></p>
  
  <p>There doesn't appear to be any functional difference between the two methods. </p>
  
  <p>Is there any difference under the hood? Any reason to prefer one or the other?</p>
  
  <p>EDIT: Adding bytecode difference.</p>
  
  <p>The bytecode generated is almost identical except for one line:</p>
  
  <p>Method 1(<code>Builder&lt;K,V&gt;</code>):</p>
  
  <pre><code>static {};
       0  new com.google.common.collect.ImmutableMap$Builder [12]
       3  dup
       4  invokespecial com.google.common.collect.ImmutableMap$Builder() [14]
       7  ldc &lt;String "key1"&gt; [17]
       ........
  </code></pre>
  
  <p>Method 2:(<code>&lt;K,V&gt;builder()</code>)</p>
  
  <pre><code>static {};
       0  invokestatic com.google.common.collect.ImmutableMap.builder() : com.google.common.collect.ImmutableMap$Builder [12]
       3  ldc &lt;String "key1"&gt; [18]
       ........
  </code></pre>
  
  <p>After that they're pretty much the same, the first line being different makes sense given the source differences, but I'm not sure why the first one calls <code>dup</code> and the second doesn't.</p>