Multiple entries with same key immutable map error
<p>I have a below builder class which I am using from multithread application so I have made it thread safe. Just for simplicity, I am showing only few fields here to demonstrate the problem.</p>

<pre><code>public final class ClientKey {
  private final long userId;
  private final int clientId;
  private final String processName;
  private final Map&lt;String, String&gt; parameterMap;

  private ClientKey(Builder builder) {
    this.userId = builder.userId;
    this.clientId = builder.clientId;
    this.processName = builder.processName;
    // initializing the required fields
    // and below line throws exception once I try to clone the `ClientKey` object
    builder.parameterMap.put("is_clientid", (clientId == 0) ? "false" : "true");
    this.parameterMap = builder.parameterMap.build();
  }

  public static class Builder {
    private final long userId;
    private final int clientId;
    private String processName;
    private ImmutableMap.Builder&lt;String, String&gt; parameterMap = ImmutableMap.builder();

    // this is for cloning
    public Builder(ClientKey key) {
      this.userId = key.userId;
      this.clientId = key.clientId;
      this.processName = key.processName;
      this.parameterMap =
          ImmutableMap.&lt;String, String&gt;builder().putAll(key.parameterMap);
    }

    public Builder(long userId, int clientId) {
      this.userId = userId;
      this.clientId = clientId;
    }

    public Builder parameterMap(Map&lt;String, String&gt; parameterMap) {
      this.parameterMap.putAll(parameterMap);
      return this;
    }

    public Builder processName(String processName) {
      this.processName = processName;
      return this;
    }

    public ClientKey build() {
      return new ClientKey(this);
    }
  }

  // getters
}
</code></pre>

<p>Below is how I make <code>ClientKey</code> and it works fine.</p>

<pre><code>Map&lt;String, String&gt; testMap = new HashMap&lt;String, String&gt;();
testMap.put("hello", "world");
ClientKey keys = new ClientKey.Builder(12345L, 200).parameterMap(testMap).build();
</code></pre>

<p>Now when I try to clone the <code>keys</code> object as shown below, it throws exception.</p>

<pre><code>ClientKey clonedKey = new ClientKey.Builder(keys).processName("hello").build();
</code></pre>

<p>It throws exception with error message as: <code>java.lang.IllegalArgumentException: Multiple entries with same key: is_clientid=true and is_clientid=true</code></p>

<pre><code>builder.parameterMap.put("is_clientid", (clientId == 0) ? "false" : "true");
// from below line exception is coming
this.parameterMap = builder.parameterMap.build();
</code></pre>

<p>How can I fix this problem? I want to make my map immutable but I also want to initialize with required fields as well and that I can only do it in the constructor of <code>ClientKey</code> class. And it throws exception while cloning the <code>ClientKey</code> object.</p>