title: Map allowing to putIfAbsent without creating the value when key already exists
body: <p>I'd like to use a map that would be equivalent to <code>ConcurrentMap</code> (I want the equivalent of the <em>putIfAbsent</em> method) but that would not force me to create the object beforehand.</p>
  
  <p>For example when I do this:</p>
  
  <pre><code>m.putIfAbsent( key, new CyclingArray() );
  </code></pre>
  
  <p>I may end up creating a new <em>CyclingArray</em> (whatever that is) object for nothing.</p>
  
  <p>Of course I realize I could lock up the entire map but that would be defeating the whole point of a <code>ConcurrentMap</code>.</p>
  
  <p>Could something like the following work conceptually?</p>
  
  <pre><code>   m.putIfAbsent( key, new Callback&lt;CyclingArray&gt;() {
       @Override
       public CyclingArray provide() {
         return new CyclingArray();  // only called if the key wasn't already present
       }         
     }
  </code></pre>
  
  <p>Do you know of any library offering a map that:</p>
  
  <ol>
  <li>offer an "interface" like the one offered by ConcurrentMap, including a <code>putIfAbsent</code> method.</li>
  <li>does only lock on the segment we're going to work with (like the <code>ConcurrentHashMap</code> implementation does, for example)</li>
  <li>allows to optionally create the value, if and only if the key wasn't already present and hence dodge useless garbage generation.</li>
  <li>does not force me to use first a <em>containsKey</em> and then a <em>putIfAbsent</em> because this, also, somehow defeats the purpose of <em>putIfAbsent</em>.</li>
  </ol>
  
  <p>Note that I'm <strong>not</strong> asking if the above example can be done with a <code>ConcurrentMap</code> (it cannot AFAIK).</p>
  
  <p>I was thinking about extending <code>ConcurrentHashMap</code> and overloading <em>putIfAbsent</em> with the callback version but sadly <code>ConcurrentHashMap</code> internally uses a final <em>Segment</em> class.</p>
  
  <p>Before re-inventing the wheel I'd like to know if there are any maps out there already offering a similar functionality.</p>