public static <K,V>V atomicGetOrCreate(ConcurrentMap<K,V> map,K key,Callable<V> ctor){
  V value=map.get(key);
  if (value == null) {
    try {
      value=ctor.call();
    }
 catch (    Exception e) {
      throw new RuntimeException("Element constructor should now throw an exception",e);
    }
    V existing=map.putIfAbsent(key,value);
    if (existing != null) {
      return existing;
    }
  }
  return value;
}

public static < K , V > V atomicGetOrCreate ( ConcurrentMap < K , V > map , K key , Callable < V > ctor ) { V value = map . get ( key ) ; if ( value == null ) { try { value = ctor . call ( ) ; } catch ( Exception e ) { throw new RuntimeException ( "Element constructor should now throw an exception" , e ) ; } V existing = map . putIfAbsent ( key , value ) ; if ( existing != null ) { return existing ; } } return value ; }
atomically returns a map element for the specifying key . a new instance of value is created if it is missing . this may be efficiently used for creating map of maps / sets . < p / > this method is thread - safe .
