WeakConcurrentMap extends ReferenceQueue<K> { @Nullable public V putIfProbablyAbsent(K key, V value) { if (key == null) throw new NullPointerException("key == null"); if (value == null) throw new NullPointerException("value == null"); expungeStaleEntries(); return target.putIfAbsent(new WeakKey<>(key, this), value); }  @Nullable V getIfPresent(K key); @Nullable V putIfProbablyAbsent(K key, V value); @Nullable V remove(K key); @Override String toString();  }
@Test public void getOrCreate_whenSomeReferencesAreCleared() { map.putIfProbablyAbsent(key, "1"); pretendGCHappened(); map.putIfProbablyAbsent(key, "1"); assertThat(map.target.keySet()) .extracting(WeakReference::get) .containsExactlyInAnyOrder(null, key); }