Can a java Map return a size of -1?
<p>I have some code that creates a List, initialized with the size of a Map:</p>

<pre><code>private Set&lt;String&gt; mKeys = new HashSet&lt;String&gt;(64);
....
List&lt;String&gt; keyList = new ArrayList&lt;String&gt;(mKeys.size());
</code></pre>

<p>I am seeing an exception: java.lang.IllegalArgumentException: Illegal Capacity: -1</p>

<p>Can a Map return a size of -1? I am looking at the source code for HashSet, which backed by a HashMap. The source code for HashMap shows the internals where elementCount is always decremented on a removeEntry() call. Also, the methods for HashMap.empty() reply on elementCount being == 0, which would return false if elementCount was -1.</p>

<p>Has anyone run into this before? I can code around it, but that feels like a hack, which makes me think I am doing something wrong with the current code.</p>

<p>EDIT: I was trying to simplify the problem originally. The Set I'm using is actually defined as</p>

<pre><code>private static Set&lt;String&gt; mKeys = Collections.synchronizedSet(new HashSet&lt;String&gt;(64));
</code></pre>

<p>EDIT: The key here may be in the synchronizedSet. From the JavaDoc</p>

<pre><code>It is imperative that the user manually synchronize on the returned set when iterating over it:

  Set s = Collections.synchronizedSet(new HashSet());
      ...
  synchronized(s) {
      Iterator i = s.iterator(); // Must be in the synchronized block
      while (i.hasNext())
          foo(i.next());
  }

Failure to follow this advice may result in non-deterministic behavior.
</code></pre>

<p>Non-deterministic behavior to me might include a size of -1. I need to go back and make sure I an synchronizing correctly when iterating over the set, but I suspect this is the problem.</p>
