java.util.WeakHashMap类的使用及代码示例

x33g5p2x  于2022-02-02 转载在 其他  
字(7.2k)|赞(0)|评价(0)|浏览(124)

本文整理了Java中java.util.WeakHashMap类的一些代码示例,展示了WeakHashMap类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WeakHashMap类的具体详情如下:
包路径:java.util.WeakHashMap
类名称:WeakHashMap

WeakHashMap介绍

[英]WeakHashMap is an implementation of Map with keys which are WeakReferences. A key/value mapping is removed when the key is no longer referenced. All optional operations (adding and removing) are supported. Keys and values can be any objects. Note that the garbage collector acts similar to a second thread on this collection, possibly removing keys.
[中]WeakHashMap是Map的一个实现,其键是WeakReference。当不再引用键时,键/值映射将被删除。支持所有可选操作(添加和删除)。键和值可以是任何对象。请注意,垃圾收集器的行为类似于此集合上的第二个线程,可能会删除密钥。

代码示例

代码示例来源:origin: robolectric/robolectric

private static Map<String, Object> get(ContentResolver cr) {
  Map<String, Object> map = dataMap.get(cr);
  if (map == null) {
   map = new HashMap<>();
   dataMap.put(cr, map);
  }
  return map;
 }
}

代码示例来源:origin: netty/netty

@Override
  protected Map<Stack<?>, WeakOrderQueue> initialValue() {
    return new WeakHashMap<Stack<?>, WeakOrderQueue>();
  }
};

代码示例来源:origin: apache/geode

@Override
public synchronized V remove(Object key) {
 WeakHashMap<K, V> tmp = new WeakHashMap<K, V>(map);
 V result = tmp.remove(key);
 map = Collections.unmodifiableMap(tmp);
 return result;
}

代码示例来源:origin: apache/geode

@Override
public synchronized V put(K key, V value) {
 WeakHashMap<K, V> tmp = new WeakHashMap<K, V>(map);
 V result = tmp.put(key, value);
 map = Collections.unmodifiableMap(tmp);
 return result;
}

代码示例来源:origin: com.sun.xml.bind/jaxb-impl

/**
 * Computes the edit distance between two strings.
 * 
 * <p>
 * The complexity is O(nm) where n=a.length() and m=b.length().
 */
public static int editDistance( String a, String b ) {
  // let's check cache
  AbstractMap.SimpleEntry<String,String> entry = new AbstractMap.SimpleEntry<String, String>(a, b); // using this class to avoid creation of my own which will handle PAIR of values
  Integer result = null;
  if (CACHE.containsKey(entry))
    result = CACHE.get(entry); // looks like we have it
  if (result == null) {
    result = new EditDistance(a, b).calc();
    CACHE.put(entry, result); // cache the result
  }
  return result;
}

代码示例来源:origin: fengjiachun/Jupiter

static AccessorClassLoader get(Class<?> type) {
  ClassLoader parent = getParentClassLoader(type);
  // 1. 最快路径:
  if (selfContextParentClassLoader.equals(parent)) {
    if (selfContextAccessorClassLoader == null) {
      synchronized (accessorClassLoaders) { // DCL with volatile semantics
        if (selfContextAccessorClassLoader == null)
          selfContextAccessorClassLoader = new AccessorClassLoader(selfContextParentClassLoader);
      }
    }
    return selfContextAccessorClassLoader;
  }
  // 2. 常规查找:
  synchronized (accessorClassLoaders) {
    WeakReference<AccessorClassLoader> ref = accessorClassLoaders.get(parent);
    if (ref != null) {
      AccessorClassLoader accessorClassLoader = ref.get();
      if (accessorClassLoader != null) {
        return accessorClassLoader;
      } else {
        accessorClassLoaders.remove(parent); // the value has been GC-reclaimed, but still not the key (defensive sanity)
      }
    }
    AccessorClassLoader accessorClassLoader = new AccessorClassLoader(parent);
    accessorClassLoaders.put(parent, new WeakReference<>(accessorClassLoader));
    return accessorClassLoader;
  }
}

代码示例来源:origin: stackoverflow.com

WeakHashMap<Data, String> map = new WeakHashMap<Data, String>();
Data someDataObject = new Data("foo");
map.put(someDataObject, someDataObject.value);
System.out.println("map contains someDataObject ? " + map.containsKey(someDataObject));
  if (map.size() != 0) {
    System.out.println("At iteration " + i + " the map still holds the reference on someDataObject");
  } else {

代码示例来源:origin: apache/flink

/** A new configuration where the behavior of reading from the default
 * resources can be turned off.
 *
 * If the parameter {@code loadDefaults} is false, the new instance
 * will not load resources from the default files.
 * @param loadDefaults specifies whether to load from the default files
 */
public Configuration(boolean loadDefaults) {
  this.loadDefaults = loadDefaults;
  synchronized(Configuration.class) {
    REGISTRY.put(this, null);
  }
}

代码示例来源:origin: google/guava

public void testAwaitDone_FinalizationPredicate() {
 final WeakHashMap<Object, Object> map = new WeakHashMap<>();
 map.put(new Object(), Boolean.TRUE);
 GcFinalization.awaitDone(
   new FinalizationPredicate() {
    public boolean isDone() {
     return map.isEmpty();
    }
   });
 assertTrue(map.isEmpty());
}

代码示例来源:origin: grantland/android-autofittextview

/**
 * Returns the {@link AutofitHelper} for this child View.
 */
public AutofitHelper getAutofitHelper(TextView textView) {
  return mHelpers.get(textView);
}

代码示例来源:origin: jfoenixadmin/JFoenix

@Override
public void cache(Node node) {
  if (!cache.containsKey(node)) {
    final CacheMemento cacheMemento = new CacheMemento(node);
    cache.put(node, cacheMemento);
    cacheMemento.cache();
  }
}

代码示例来源:origin: androidquery/androidquery

@Override
public final void callback(String url, Bitmap bm, AjaxStatus status) {
  
  ImageView firstView = v.get();
  WeakHashMap<ImageView, BitmapAjaxCallback> ivs = queueMap.remove(url);
  
  //check if view queue already contains first view 
  if(ivs == null || !ivs.containsKey(firstView)){
    checkCb(this, url, firstView, bm, status);
  }
  
  if(ivs != null){
  
    Set<ImageView> set = ivs.keySet();
    
    for(ImageView view: set){
      BitmapAjaxCallback cb = ivs.get(view);
      cb.status = status;                
      checkCb(cb, url, view, bm, status);
    }
  
  }
  
}

代码示例来源:origin: mabe02/lanterna

private static synchronized void removeTaskFromTimer(AnimatedLabel animatedLabel) {
  SCHEDULED_TASKS.get(animatedLabel).cancel();
  SCHEDULED_TASKS.remove(animatedLabel);
  canCloseTimer();
}

代码示例来源:origin: grandcentrix/tray

mListeners.put(listener, handler);
final Collection<OnTrayPreferenceChangeListener> listeners = mListeners.keySet();

代码示例来源:origin: alibaba/jvm-sandbox

final Integer objectID = objectIDMapping.get(object);
  if (null != objectID) {
    return objectID;
try {
  final Integer nextObjectID;
  if (objectIDMapping.containsKey(object)) {
    nextObjectID = objectIDMapping.get(object);
  } else {
    mapping(

代码示例来源:origin: android-hacker/VirtualXposed

public void removePackage(String packageName) {
  synchronized (this) {
    mPackages.remove(packageName);
  }
}

代码示例来源:origin: apache/flink

/**
 * Reload existing configuration instances.
 */
public static synchronized void reloadExistingConfigurations() {
  if (LOG.isDebugEnabled()) {
    LOG.debug("Reloading " + REGISTRY.keySet().size()
        + " existing configurations");
  }
  for (Configuration conf : REGISTRY.keySet()) {
    conf.reloadConfiguration();
  }
}

代码示例来源:origin: apache/geode

/**
 * Throws LeaseExpiredException if the calling thread's lease on this lock previously expired. The
 * expired lease will no longer be tracked after throwing LeaseExpiredException. Caller must
 * synchronize on this lock token.
 *
 * @throws LeaseExpiredException if calling thread's lease expired
 */
void throwIfCurrentThreadHadExpiredLease() throws LeaseExpiredException {
 if (this.expiredLeases == null) {
  return;
 }
 if (this.expiredLeases.containsKey(Thread.currentThread())) {
  this.expiredLeases.remove(Thread.currentThread());
  throw new LeaseExpiredException(
    "This thread's lease expired for this lock");
 }
}

代码示例来源:origin: grandcentrix/tray

public void unregisterOnTrayPreferenceChangeListener(
    @NonNull final OnTrayPreferenceChangeListener listener) {
  // noinspection ConstantConditions
  if (listener == null) {
    return;
  }
  mListeners.remove(listener);
  if (mListeners.size() == 0) {
    mContext.getContentResolver().unregisterContentObserver(mObserver);
    // cleanup
    mObserver = null;
    mObserverThread.quit();
    mObserverThread = null;
  }
}

代码示例来源:origin: orbit/orbit

public synchronized void clearTimers()
{
  if (timers != null)
  {
    timers.keySet().stream()
        .filter(r -> r != null)
        .forEach(Registration::dispose);
    timers.clear();
    timers = null;
  }
}

相关文章