title: Which implementation of Map<K,V> should I use if my map needs to be small more than fast?
body: <p>I habitually use <code>HashMap</code> in my programs, since I know it is usually the most efficient (if properly used) and can cope with large maps easily. I know about <code>EnumMap</code> which is very useful for enumeration keys, but often I am generating a small map which will never get very big, is likely to be discarded pretty soon, and has no concurrency issues.</p>
  
  <p>Is <code>HashMap&lt;K,V&gt;</code> too complicated for these small, local and temporary uses? Is there another, simple, implementation which I can use in these cases?</p>
  
  <p>I think I'm looking for a <code>Map</code> implementation which is analogous to <code>ArrayList</code> for <code>List</code>. Does it exist?</p>
  
  <hr>
  
  <p>Added later after responses:</p>
  
  <p>Here is a scenario where a slow but very simple implementation <i>might</i> be better -- when I have many, many of these <code>Map</code>s.  Suppose, for example, I have a million or so of these tiny little maps, each with a handful (often less than three) of entries.  I have a low reference rate -- perhaps I don't actually reference them before they are discarded most of the time.  Is it still the case that <code>HashMap</code> is the best choice for them?</p>
  
  <p>Resource utilisation is more than just speed -- I would like something that doesn't fragment the heap a lot and make GCs take a long time, for example.</p>
  
  <p>It may be that <code>HashMap</code> is the right answer, but this is not a case of premature optimisation (or at least it may not be).</p>
  
  <hr>
  
  <p><em>Added much later after some thought:</em></p>
  
  <p>I decided to hand-code my own <code>SmallMap</code>. It is easy to make one with <code>AbstractMap</code>.  I have also added a couple of constructors so that a <code>SmallMap</code> can be constructed from an existing <code>Map</code>.</p>
  
  <p>Along the way I had to decide how to represent <code>Entry</code>s and to implement <code>SmallSet</code> for the <code>entrySet</code> method.</p>
  
  <p>I learned a lot by coding (and unit-testing this) and want to share this, in case anyone else wants one. It is on github <a href="http://github.com/Zteve/SmallCollections" rel="noreferrer">here</a>.</p>