
com.magictools.core.map.FixedLinkedHashMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of magic-tools-all Show documentation
Show all versions of magic-tools-all Show documentation
magic-tools 是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。
The newest version!
package com.magictools.core.map;
import java.util.LinkedHashMap;
/**
* 固定大小的{@link LinkedHashMap} 实现
* 注意此类非线程安全,由于{@link #get(Object)}操作会修改链表的顺序结构,因此也不可以使用读写锁。
*
* @author looly
*
* @param 键类型
* @param 值类型
*/
public class FixedLinkedHashMap extends LinkedHashMap {
private static final long serialVersionUID = -629171177321416095L;
/** 容量,超过此容量自动删除末尾元素 */
private int capacity;
/**
* 构造
*
* @param capacity 容量,实际初始容量比容量大1
*/
public FixedLinkedHashMap(int capacity) {
super(capacity + 1, 1.0f, true);
this.capacity = capacity;
}
/**
* 获取容量
*
* @return 容量
*/
public int getCapacity() {
return this.capacity;
}
/**
* 设置容量
*
* @param capacity 容量
*/
public void setCapacity(int capacity) {
this.capacity = capacity;
}
@Override
protected boolean removeEldestEntry(java.util.Map.Entry eldest) {
//当链表元素大于容量时,移除最老(最久未被使用)的元素
return size() > this.capacity;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy