java.util.LinkedHashMap.remove()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.7k)|赞(0)|评价(0)|浏览(234)

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

LinkedHashMap.remove介绍

[英]Returns true if this map should remove its eldest entry. This method is invoked by put and putAll after inserting a new entry into the map. It provides the implementor with the opportunity to remove the eldest entry each time a new one is added. This is useful if the map represents a cache: it allows the map to reduce memory consumption by deleting stale entries.

Sample use: this override will allow the map to grow up to 100 entries and then delete the eldest entry each time a new entry is added, maintaining a steady state of 100 entries.

private static final int MAX_ENTRIES = 100; 
protected boolean removeEldestEntry(Map.Entry eldest) { 
return size() > MAX_ENTRIES; 
}

This method typically does not modify the map in any way, instead allowing the map to modify itself as directed by its return value. It is permitted for this method to modify the map directly, but if it does so, it must return false (indicating that the map should not attempt any further modification). The effects of returning true after modifying the map from within this method are unspecified.

This implementation merely returns false (so that this map acts like a normal map - the eldest element is never removed).
[中]如果此映射应删除其最早的条目,则返回true。将新条目插入映射后,put和putAll将调用此方法。它为实现者提供了在每次添加新条目时删除最旧条目的机会。如果映射表示缓存,则这非常有用:它允许映射通过删除过时的条目来减少内存消耗。
示例使用:此覆盖将允许贴图最多增加100个条目,然后在每次添加新条目时删除最老的条目,从而保持100个条目的稳定状态。

private static final int MAX_ENTRIES = 100; 
protected boolean removeEldestEntry(Map.Entry eldest) { 
return size() > MAX_ENTRIES; 
}

此方法通常不会以任何方式修改映射,而是允许映射按照其返回值的指示修改自身。允许此方法直接修改映射,但如果这样做,则必须返回false(表示映射不应尝试进一步修改)。未指定在此方法中修改贴图后返回true的效果。
此实现仅返回false(因此此映射的行为类似于普通映射-最老的元素永远不会被删除)。

代码示例

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

public void moveToEnd(TopicPartition topicPartition) {
  S state = map.remove(topicPartition);
  if (state != null)
    map.put(topicPartition, state);
}

代码示例来源:origin: square/okhttp

key = line.substring(keyBegin);
 if (firstSpace == REMOVE.length() && line.startsWith(REMOVE)) {
  lruEntries.remove(key);
  return;
Entry entry = lruEntries.get(key);
if (entry == null) {
 entry = new Entry(key);
 lruEntries.put(key, entry);

代码示例来源:origin: nostra13/Android-Universal-Image-Loader

/**
 * Drops the entry for {@code key} if it exists and can be removed. Entries
 * actively being edited cannot be removed.
 *
 * @return true if an entry was removed.
 */
public synchronized boolean remove(String key) throws IOException {
  checkNotClosed();
  validateKey(key);
  Entry entry = lruEntries.get(key);
  if (entry == null || entry.currentEditor != null) {
    return false;
  }
  for (int i = 0; i < valueCount; i++) {
    File file = entry.getCleanFile(i);
    if (file.exists() && !file.delete()) {
      throw new IOException("failed to delete " + file);
    }
    size -= entry.lengths[i];
    fileCount--;
    entry.lengths[i] = 0;
  }
  redundantOpCount++;
  journalWriter.append(REMOVE + ' ' + key + '\n');
  lruEntries.remove(key);
  if (journalRebuildRequired()) {
    executorService.submit(cleanupCallable);
  }
  return true;
}

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

targetPathToAliases.remove(pathToRemove);
targetPathToPartitionInfo.remove(pathToRemove);
 targetPathToAliases.put(pathToAdd, new ArrayList<String>());
targetPathToAliases.get(pathToAdd).add(sourceAlias);

代码示例来源:origin: alibaba/mdrill

/**
 * Adds an extra field - replacing an already present extra field
 * of the same type.
 *
 * <p>The new extra field will be the first one.</p>
 * @param ze an extra field
 * @since 1.1
 */
public void addAsFirstExtraField(ZipExtraField ze) {
  if (ze instanceof UnparseableExtraFieldData) {
    unparseableExtra = (UnparseableExtraFieldData) ze;
  } else {
    LinkedHashMap<ZipShort, ZipExtraField> copy = extraFields;
    extraFields = new LinkedHashMap<ZipShort, ZipExtraField>();
    extraFields.put(ze.getHeaderId(), ze);
    if (copy != null) {
      copy.remove(ze.getHeaderId());
      extraFields.putAll(copy);
    }
  }
  setExtra();
}

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

@Override
protected void writeContext(ObjectOutput output, LinkedHashMap<Object, Object> map) throws IOException {
  Object insertOrder = new Object();
  Object accessOrder = new Object();
  map.put(insertOrder, null);
  map.put(accessOrder, null);
  // Access first inserted entry
  // If map uses access order, this element will move to the tail of the map
  map.get(insertOrder);
  Iterator<Object> keys = map.keySet().iterator();
  Object element = keys.next();
  while ((element != insertOrder) && (element != accessOrder)) {
    element = keys.next();
  }
  map.remove(insertOrder);
  map.remove(accessOrder);
  // Map uses access order if previous access changed iteration order
  output.writeBoolean(element == accessOrder);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public V put(String key, @Nullable V value) {
  String oldKey = this.caseInsensitiveKeys.put(convertKey(key), key);
  V oldKeyValue = null;
  if (oldKey != null && !oldKey.equals(key)) {
    oldKeyValue = this.targetMap.remove(oldKey);
  }
  V oldValue = this.targetMap.put(key, value);
  return (oldKeyValue != null ? oldKeyValue : oldValue);
}

代码示例来源:origin: k9mail/k-9

public void onClickRemoveAttachment(Uri uri) {
  Attachment attachment = attachments.get(uri);
  loaderManager.destroyLoader(attachment.loaderId);
  attachmentMvpView.removeAttachmentView(attachment);
  attachments.remove(uri);
  listener.onAttachmentRemoved();
}

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

targetPathToAliases.remove(pathToRemove);
targetPathToPartitionInfo.remove(pathToRemove);
 targetPathToAliases.put(pathToAdd, new ArrayList<String>());
targetPathToAliases.get(pathToAdd).add(sourceAlias);

代码示例来源:origin: nostra13/Android-Universal-Image-Loader

key = line.substring(keyBegin);
  if (firstSpace == REMOVE.length() && line.startsWith(REMOVE)) {
    lruEntries.remove(key);
    return;
Entry entry = lruEntries.get(key);
if (entry == null) {
  entry = new Entry(key);
  lruEntries.put(key, entry);

代码示例来源:origin: SonarSource/sonarqube

@Override
public Profiler addContext(String key, @Nullable Object value) {
 if (value == null) {
  context.remove(key);
 } else {
  context.put(key, value);
 }
 return this;
}

代码示例来源:origin: JakeWharton/DiskLruCache

/**
 * Drops the entry for {@code key} if it exists and can be removed. Entries
 * actively being edited cannot be removed.
 *
 * @return true if an entry was removed.
 */
public synchronized boolean remove(String key) throws IOException {
 checkNotClosed();
 validateKey(key);
 Entry entry = lruEntries.get(key);
 if (entry == null || entry.currentEditor != null) {
  return false;
 }
 for (int i = 0; i < valueCount; i++) {
  File file = entry.getCleanFile(i);
  if (file.exists() && !file.delete()) {
   throw new IOException("failed to delete " + file);
  }
  size -= entry.lengths[i];
  entry.lengths[i] = 0;
 }
 redundantOpCount++;
 journalWriter.append(REMOVE + ' ' + key + '\n');
 lruEntries.remove(key);
 if (journalRebuildRequired()) {
  executorService.submit(cleanupCallable);
 }
 return true;
}

代码示例来源:origin: prestodb/presto

key = line.substring(keyBegin);
 if (firstSpace == REMOVE.length() && line.startsWith(REMOVE)) {
  lruEntries.remove(key);
  return;
Entry entry = lruEntries.get(key);
if (entry == null) {
 entry = new Entry(key);
 lruEntries.put(key, entry);

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

public void processPrepare(TransactionId key) {
  synchronized (indexMutex) {
    ArrayList<Operation> tx = inflightTransactions.remove(key);
    if (tx != null) {
      preparedTransactions.put(key, tx);
    }
  }
}

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

aliasName = service_alias.substring(i + 1);
if (propertyAliasTable != null) {
  propertyAliasTable.remove(key(serviceName, aliasName));
algorithm = k.substring(j + 1);
if (propertyServiceTable != null) {
  Provider.Service ser = propertyServiceTable.remove(key(serviceName, algorithm));
  if (ser != null && propertyAliasTable != null
      && ser.aliases != null) {
    for (String alias : ser.aliases) {
      propertyAliasTable.remove(key(serviceName, alias));
algorithm = k.substring(j + 1, i);
if (propertyServiceTable != null) {
  Object o = propertyServiceTable.get(key(serviceName, algorithm));
  if (o != null) {
    s = (Provider.Service) o;

代码示例来源:origin: JakeWharton/DiskLruCache

key = line.substring(keyBegin);
 if (firstSpace == REMOVE.length() && line.startsWith(REMOVE)) {
  lruEntries.remove(key);
  return;
Entry entry = lruEntries.get(key);
if (entry == null) {
 entry = new Entry(key);
 lruEntries.put(key, entry);

代码示例来源:origin: SonarSource/sonarqube

@Override
public Profiler addContext(String key, @Nullable Object value) {
 if (value == null) {
  context.remove(key);
 } else {
  context.put(key, value);
 }
 return this;
}

代码示例来源:origin: vsch/flexmark-java

public Attribute remove(CharSequence key) {
  if (myAttributes == null || key == null || key.length() == 0) return null;
  String useKey = String.valueOf(key);
  Attribute oldAttribute = myAttributes.get(useKey);
  myAttributes.remove(useKey);
  return oldAttribute;
}

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

TopicPartition topicPartition = entry.getKey();
PartitionData prevData = entry.getValue();
PartitionData nextData = next.get(topicPartition);
if (nextData != null) {
  if (prevData.equals(nextData)) {
    next.remove(topicPartition);
  } else {
    next.remove(topicPartition);
    next.put(topicPartition, nextData);
    entry.setValue(nextData);
    altered.add(topicPartition);
sessionPartitions.put(topicPartition, nextData);
added.add(topicPartition);

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

public void updateAndMoveToEnd(TopicPartition topicPartition, S state) {
  map.remove(topicPartition);
  map.put(topicPartition, state);
  updateSize();
}

相关文章