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

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

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

LinkedHashMap.removeEldestEntry介绍

[英]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).
[中]

代码示例

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

@Override void addNewEntryForNullKey(V value) {
  LinkedEntry<K, V> header = this.header;
  // Remove eldest entry if instructed to do so.
  LinkedEntry<K, V> eldest = header.nxt;
  if (eldest != header && removeEldestEntry(eldest)) {
    remove(eldest.key);
  }
  // Create new entry, link it on to list, and put it into table
  LinkedEntry<K, V> oldTail = header.prv;
  LinkedEntry<K, V> newTail = new LinkedEntry<K,V>(
      null, value, 0, null, header, oldTail);
  entryForNullKey = oldTail.nxt = header.prv = newTail;
}

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

/**
 * Evicts eldest entry if instructed, creates a new entry and links it in
 * as head of linked list. This method should call constructorNewEntry
 * (instead of duplicating code) if the performance of your VM permits.
 *
 * <p>It may seem strange that this method is tasked with adding the entry
 * to the hash table (which is properly the province of our superclass).
 * The alternative of passing the "next" link in to this method and
 * returning the newly created element does not work! If we remove an
 * (eldest) entry that happens to be the first entry in the same bucket
 * as the newly created entry, the "next" link would become invalid, and
 * the resulting hash table corrupt.
 */
@Override void addNewEntry(K key, V value, int hash, int index) {
  LinkedEntry<K, V> header = this.header;
  // Remove eldest entry if instructed to do so.
  LinkedEntry<K, V> eldest = header.nxt;
  if (eldest != header && removeEldestEntry(eldest)) {
    remove(eldest.key);
  }
  // Create new entry, link it on to list, and put it into table
  LinkedEntry<K, V> oldTail = header.prv;
  LinkedEntry<K, V> newTail = new LinkedEntry<K,V>(
      key, value, hash, table[index], header, oldTail);
  table[index] = oldTail.nxt = header.prv = newTail;
}

代码示例来源:origin: org.apache.tomee.patch/bval-jsr

@Override
  protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
    return super.removeEldestEntry(eldest) || size() >= maximumCapacity;
  }
}

代码示例来源:origin: org.apache.bval/bval-jsr

@Override
  protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
    return super.removeEldestEntry(eldest) || size() >= maximumCapacity;
  }
}

代码示例来源:origin: com.mobidevelop.robovm/robovm-rt

@Override void addNewEntryForNullKey(V value) {
  LinkedEntry<K, V> header = this.header;
  // Remove eldest entry if instructed to do so.
  LinkedEntry<K, V> eldest = header.nxt;
  if (eldest != header && removeEldestEntry(eldest)) {
    remove(eldest.key);
  }
  // Create new entry, link it on to list, and put it into table
  LinkedEntry<K, V> oldTail = header.prv;
  LinkedEntry<K, V> newTail = new LinkedEntry<K,V>(
      null, value, 0, null, header, oldTail);
  entryForNullKey = oldTail.nxt = header.prv = newTail;
}

代码示例来源:origin: ibinti/bugvm

@Override void addNewEntryForNullKey(V value) {
  LinkedEntry<K, V> header = this.header;
  // Remove eldest entry if instructed to do so.
  LinkedEntry<K, V> eldest = header.nxt;
  if (eldest != header && removeEldestEntry(eldest)) {
    remove(eldest.key);
  }
  // Create new entry, link it on to list, and put it into table
  LinkedEntry<K, V> oldTail = header.prv;
  LinkedEntry<K, V> newTail = new LinkedEntry<K,V>(
      null, value, 0, null, header, oldTail);
  entryForNullKey = oldTail.nxt = header.prv = newTail;
}

代码示例来源:origin: xyz.cofe/common

@Override
  protected boolean removeEldestEntry(java.util.Map.Entry<K, V> eldest) {
    if( cacheSizeMax>0 ){
      return size() > cacheSizeMax;
    }
    return super.removeEldestEntry(eldest);
  }
}

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

@Override void addNewEntryForNullKey(V value) {
  LinkedEntry<K, V> header = this.header;
  // Remove eldest entry if instructed to do so.
  LinkedEntry<K, V> eldest = header.nxt;
  if (eldest != header && removeEldestEntry(eldest)) {
    remove(eldest.key);
  }
  // Create new entry, link it on to list, and put it into table
  LinkedEntry<K, V> oldTail = header.prv;
  LinkedEntry<K, V> newTail = new LinkedEntry<K,V>(
      null, value, 0, null, header, oldTail);
  entryForNullKey = oldTail.nxt = header.prv = newTail;
}

代码示例来源:origin: FlexoVM/flexovm

@Override void addNewEntryForNullKey(V value) {
  LinkedEntry<K, V> header = this.header;
  // Remove eldest entry if instructed to do so.
  LinkedEntry<K, V> eldest = header.nxt;
  if (eldest != header && removeEldestEntry(eldest)) {
    remove(eldest.key);
  }
  // Create new entry, link it on to list, and put it into table
  LinkedEntry<K, V> oldTail = header.prv;
  LinkedEntry<K, V> newTail = new LinkedEntry<K,V>(
      null, value, 0, null, header, oldTail);
  entryForNullKey = oldTail.nxt = header.prv = newTail;
}

代码示例来源:origin: com.bugvm/bugvm-rt

@Override void addNewEntryForNullKey(V value) {
  LinkedEntry<K, V> header = this.header;
  // Remove eldest entry if instructed to do so.
  LinkedEntry<K, V> eldest = header.nxt;
  if (eldest != header && removeEldestEntry(eldest)) {
    remove(eldest.key);
  }
  // Create new entry, link it on to list, and put it into table
  LinkedEntry<K, V> oldTail = header.prv;
  LinkedEntry<K, V> newTail = new LinkedEntry<K,V>(
      null, value, 0, null, header, oldTail);
  entryForNullKey = oldTail.nxt = header.prv = newTail;
}

代码示例来源:origin: com.jtransc/jtransc-rt

@Override void addNewEntryForNullKey(V value) {
  LinkedEntry<K, V> header = this.header;
  // Remove eldest entry if instructed to do so.
  LinkedEntry<K, V> eldest = header.nxt;
  if (eldest != header && removeEldestEntry(eldest)) {
    remove(eldest.key);
  }
  // Create new entry, link it on to list, and put it into table
  LinkedEntry<K, V> oldTail = header.prv;
  LinkedEntry<K, V> newTail = new LinkedEntry<K,V>(
      null, value, 0, null, header, oldTail);
  entryForNullKey = oldTail.nxt = header.prv = newTail;
}

代码示例来源:origin: com.gluonhq/robovm-rt

@Override void addNewEntryForNullKey(V value) {
  LinkedEntry<K, V> header = this.header;
  // Remove eldest entry if instructed to do so.
  LinkedEntry<K, V> eldest = header.nxt;
  if (eldest != header && removeEldestEntry(eldest)) {
    remove(eldest.key);
  }
  // Create new entry, link it on to list, and put it into table
  LinkedEntry<K, V> oldTail = header.prv;
  LinkedEntry<K, V> newTail = new LinkedEntry<K,V>(
      null, value, 0, null, header, oldTail);
  entryForNullKey = oldTail.nxt = header.prv = newTail;
}

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

/**
 * Evicts eldest entry if instructed, creates a new entry and links it in
 * as head of linked list. This method should call constructorNewEntry
 * (instead of duplicating code) if the performance of your VM permits.
 *
 * <p>It may seem strange that this method is tasked with adding the entry
 * to the hash table (which is properly the province of our superclass).
 * The alternative of passing the "next" link in to this method and
 * returning the newly created element does not work! If we remove an
 * (eldest) entry that happens to be the first entry in the same bucket
 * as the newly created entry, the "next" link would become invalid, and
 * the resulting hash table corrupt.
 */
@Override void addNewEntry(K key, V value, int hash, int index) {
  LinkedEntry<K, V> header = this.header;
  // Remove eldest entry if instructed to do so.
  LinkedEntry<K, V> eldest = header.nxt;
  if (eldest != header && removeEldestEntry(eldest)) {
    remove(eldest.key);
  }
  // Create new entry, link it on to list, and put it into table
  LinkedEntry<K, V> oldTail = header.prv;
  LinkedEntry<K, V> newTail = new LinkedEntry<K,V>(
      key, value, hash, table[index], header, oldTail);
  table[index] = oldTail.nxt = header.prv = newTail;
}

代码示例来源:origin: ibinti/bugvm

/**
 * Evicts eldest entry if instructed, creates a new entry and links it in
 * as head of linked list. This method should call constructorNewEntry
 * (instead of duplicating code) if the performance of your VM permits.
 *
 * <p>It may seem strange that this method is tasked with adding the entry
 * to the hash table (which is properly the province of our superclass).
 * The alternative of passing the "next" link in to this method and
 * returning the newly created element does not work! If we remove an
 * (eldest) entry that happens to be the first entry in the same bucket
 * as the newly created entry, the "next" link would become invalid, and
 * the resulting hash table corrupt.
 */
@Override void addNewEntry(K key, V value, int hash, int index) {
  LinkedEntry<K, V> header = this.header;
  // Remove eldest entry if instructed to do so.
  LinkedEntry<K, V> eldest = header.nxt;
  if (eldest != header && removeEldestEntry(eldest)) {
    remove(eldest.key);
  }
  // Create new entry, link it on to list, and put it into table
  LinkedEntry<K, V> oldTail = header.prv;
  LinkedEntry<K, V> newTail = new LinkedEntry<K,V>(
      key, value, hash, table[index], header, oldTail);
  table[index] = oldTail.nxt = header.prv = newTail;
}

代码示例来源:origin: com.bugvm/bugvm-rt

/**
 * Evicts eldest entry if instructed, creates a new entry and links it in
 * as head of linked list. This method should call constructorNewEntry
 * (instead of duplicating code) if the performance of your VM permits.
 *
 * <p>It may seem strange that this method is tasked with adding the entry
 * to the hash table (which is properly the province of our superclass).
 * The alternative of passing the "next" link in to this method and
 * returning the newly created element does not work! If we remove an
 * (eldest) entry that happens to be the first entry in the same bucket
 * as the newly created entry, the "next" link would become invalid, and
 * the resulting hash table corrupt.
 */
@Override void addNewEntry(K key, V value, int hash, int index) {
  LinkedEntry<K, V> header = this.header;
  // Remove eldest entry if instructed to do so.
  LinkedEntry<K, V> eldest = header.nxt;
  if (eldest != header && removeEldestEntry(eldest)) {
    remove(eldest.key);
  }
  // Create new entry, link it on to list, and put it into table
  LinkedEntry<K, V> oldTail = header.prv;
  LinkedEntry<K, V> newTail = new LinkedEntry<K,V>(
      key, value, hash, table[index], header, oldTail);
  table[index] = oldTail.nxt = header.prv = newTail;
}

代码示例来源:origin: com.jtransc/jtransc-rt

/**
 * Evicts eldest entry if instructed, creates a new entry and links it in
 * as head of linked list. This method should call constructorNewEntry
 * (instead of duplicating code) if the performance of your VM permits.
 *
 * <p>It may seem strange that this method is tasked with adding the entry
 * to the hash table (which is properly the province of our superclass).
 * The alternative of passing the "next" link in to this method and
 * returning the newly created element does not work! If we remove an
 * (eldest) entry that happens to be the first entry in the same bucket
 * as the newly created entry, the "next" link would become invalid, and
 * the resulting hash table corrupt.
 */
@Override void addNewEntry(K key, V value, int hash, int index) {
  LinkedEntry<K, V> header = this.header;
  // Remove eldest entry if instructed to do so.
  LinkedEntry<K, V> eldest = header.nxt;
  if (eldest != header && removeEldestEntry(eldest)) {
    remove(eldest.key);
  }
  // Create new entry, link it on to list, and put it into table
  LinkedEntry<K, V> oldTail = header.prv;
  LinkedEntry<K, V> newTail = new LinkedEntry<K,V>(
      key, value, hash, table[index], header, oldTail);
  table[index] = oldTail.nxt = header.prv = newTail;
}

代码示例来源:origin: com.mobidevelop.robovm/robovm-rt

/**
 * Evicts eldest entry if instructed, creates a new entry and links it in
 * as head of linked list. This method should call constructorNewEntry
 * (instead of duplicating code) if the performance of your VM permits.
 *
 * <p>It may seem strange that this method is tasked with adding the entry
 * to the hash table (which is properly the province of our superclass).
 * The alternative of passing the "next" link in to this method and
 * returning the newly created element does not work! If we remove an
 * (eldest) entry that happens to be the first entry in the same bucket
 * as the newly created entry, the "next" link would become invalid, and
 * the resulting hash table corrupt.
 */
@Override void addNewEntry(K key, V value, int hash, int index) {
  LinkedEntry<K, V> header = this.header;
  // Remove eldest entry if instructed to do so.
  LinkedEntry<K, V> eldest = header.nxt;
  if (eldest != header && removeEldestEntry(eldest)) {
    remove(eldest.key);
  }
  // Create new entry, link it on to list, and put it into table
  LinkedEntry<K, V> oldTail = header.prv;
  LinkedEntry<K, V> newTail = new LinkedEntry<K,V>(
      key, value, hash, table[index], header, oldTail);
  table[index] = oldTail.nxt = header.prv = newTail;
}

代码示例来源:origin: jtulach/bck2brwsr

/**
 * This override alters behavior of superclass put method. It causes newly
 * allocated entry to get inserted at the end of the linked list and
 * removes the eldest entry if appropriate.
 */
void addEntry(int hash, K key, V value, int bucketIndex) {
  createEntry(hash, key, value, bucketIndex);
  // Remove eldest entry if instructed, else grow capacity if appropriate
  Entry<K,V> eldest = header.after;
  if (removeEldestEntry(eldest)) {
    removeEntryForKey(eldest.key);
  } else {
    if (size >= threshold)
      resize(2 * table.length);
  }
}

代码示例来源:origin: org.apidesign.bck2brwsr/emul

/**
 * This override alters behavior of superclass put method. It causes newly
 * allocated entry to get inserted at the end of the linked list and
 * removes the eldest entry if appropriate.
 */
void addEntry(int hash, K key, V value, int bucketIndex) {
  createEntry(hash, key, value, bucketIndex);
  // Remove eldest entry if instructed, else grow capacity if appropriate
  Entry<K,V> eldest = header.after;
  if (removeEldestEntry(eldest)) {
    removeEntryForKey(eldest.key);
  } else {
    if (size >= threshold)
      resize(2 * table.length);
  }
}

代码示例来源:origin: dragome/dragome-sdk

@Override
public V put(K key, V value)
{
  ChainEntry old= map.get(key);
  if (old == null)
  {
    ChainEntry newEntry= new ChainEntry(key, value);
    map.put(key, newEntry);
    newEntry.addToEnd();
    ChainEntry eldest= head.next;
    if (removeEldestEntry(eldest))
    {
      eldest.remove();
      map.remove(eldest.getKey());
    }
    return null;
  }
  else
  {
    V oldValue= old.getValue();
    old.setValue(value);
    recordAccess(old);
    return oldValue;
  }
}

相关文章