LRU算法
来源:互联网 发布:com域名注册多少钱 编辑:程序博客网 时间:2024/05/22 18:25
public class LRU<K, V> {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
lru();
}
static void lru() {
System.out.println();
System.out.println("===========================LRU 链表实现===========================");
LRU<Integer, String> lru = new LRU(5);
lru.put(1, "11");
lru.put(2, "12");
lru.put(3, "13");
lru.put(4, "11");
lru.put(5, "14");
System.out.println(lru.toString());
lru.put(6, "66");
System.out.println(lru.toString());
lru.get(2);
System.out.println(lru.toString());
lru.put(7, "77");
System.out.println(lru.toString());
lru.get(4);
System.out.println(lru.toString());
System.out.println();
}
private final int MAX_CACHE_SIZE;
private Entry head;
private Entry last;
private HashMap<K,Entry<K,V>> hashMap;
//初始化
public LRU(int size){
this.MAX_CACHE_SIZE = size;
hashMap = new HashMap<>();
}
//存放数据
public void put(K key,V value){
Entry entry = hashMap.get(key);
if(entry == null){//新的
if(hashMap.size()>=MAX_CACHE_SIZE){
//移除最后一个元素
hashMap.remove(last.key);
removeLast();
}
entry = new Entry();
entry.key = key;
}
entry.value = value;
//放在第一个位置
putFirst(entry);
hashMap.put(key, entry);
}
public V get(K key){
Entry entry = hashMap.get(key);
if(entry==null) return null;
putFirst(entry);
return (V) entry.value;
}
public void remove(K key){
Entry entry = hashMap.get(key);
if (entry != null) {
if (entry.pre != null) entry.pre.next = entry.next;
if (entry.next != null) entry.next.pre = entry.pre;
if (entry == head) head = entry.next;
if (entry == last) last = entry.pre;
}
hashMap.remove(key);
}
private void putFirst(Entry entry) {
// TODO Auto-generated method stub
if (entry == head) return;
//中间的节点操作
if (entry.pre != null) entry.pre.next = entry.next;
if (entry.next != null) entry.next.pre = entry.pre;
if (entry == last) last = last.pre;
if (head == null || last == null) {
head = last = entry;
return;
}
entry.next = head;
head.pre = entry;
head = entry;
entry.pre = null;
}
//
private void removeLast() {
// TODO Auto-generated method stub
if(last!=null){
last = last.pre;
if(last==null) head = null;
else last.next = null;
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Entry entry = head;
while (entry != null) {
sb.append(String.format("%s:%s ", entry.key, entry.value));
entry = entry.next;
}
return sb.toString();
}
//内部类
class Entry<K, V> {
public Entry pre;
public Entry next;
public K key;
public V value;
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
lru();
}
static void lru() {
System.out.println();
System.out.println("===========================LRU 链表实现===========================");
LRU<Integer, String> lru = new LRU(5);
lru.put(1, "11");
lru.put(2, "12");
lru.put(3, "13");
lru.put(4, "11");
lru.put(5, "14");
System.out.println(lru.toString());
lru.put(6, "66");
System.out.println(lru.toString());
lru.get(2);
System.out.println(lru.toString());
lru.put(7, "77");
System.out.println(lru.toString());
lru.get(4);
System.out.println(lru.toString());
System.out.println();
}
private final int MAX_CACHE_SIZE;
private Entry head;
private Entry last;
private HashMap<K,Entry<K,V>> hashMap;
//初始化
public LRU(int size){
this.MAX_CACHE_SIZE = size;
hashMap = new HashMap<>();
}
//存放数据
public void put(K key,V value){
Entry entry = hashMap.get(key);
if(entry == null){//新的
if(hashMap.size()>=MAX_CACHE_SIZE){
//移除最后一个元素
hashMap.remove(last.key);
removeLast();
}
entry = new Entry();
entry.key = key;
}
entry.value = value;
//放在第一个位置
putFirst(entry);
hashMap.put(key, entry);
}
public V get(K key){
Entry entry = hashMap.get(key);
if(entry==null) return null;
putFirst(entry);
return (V) entry.value;
}
public void remove(K key){
Entry entry = hashMap.get(key);
if (entry != null) {
if (entry.pre != null) entry.pre.next = entry.next;
if (entry.next != null) entry.next.pre = entry.pre;
if (entry == head) head = entry.next;
if (entry == last) last = entry.pre;
}
hashMap.remove(key);
}
private void putFirst(Entry entry) {
// TODO Auto-generated method stub
if (entry == head) return;
//中间的节点操作
if (entry.pre != null) entry.pre.next = entry.next;
if (entry.next != null) entry.next.pre = entry.pre;
if (entry == last) last = last.pre;
if (head == null || last == null) {
head = last = entry;
return;
}
entry.next = head;
head.pre = entry;
head = entry;
entry.pre = null;
}
//
private void removeLast() {
// TODO Auto-generated method stub
if(last!=null){
last = last.pre;
if(last==null) head = null;
else last.next = null;
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Entry entry = head;
while (entry != null) {
sb.append(String.format("%s:%s ", entry.key, entry.value));
entry = entry.next;
}
return sb.toString();
}
//内部类
class Entry<K, V> {
public Entry pre;
public Entry next;
public K key;
public V value;
}
}
0 0
- LRU算法
- LRU算法
- LRU算法
- LRU算法
- LRU 算法
- LRU算法
- LRU算法
- LRU算法
- LRU算法
- LRU算法
- lru算法
- LRU 算法
- LRU算法
- LRU算法
- Lru算法
- LRU算法
- LRU算法
- LRU算法
- C#获取电脑的唯一标示UUID 非CPU型号 mac
- 第14周项目1-(3)验证二叉排序树相关算法
- Java实现从Html文本中提取纯文本
- Hbuilder MUI 锁定和解除锁定屏幕方向
- HTML5基础总结
- LRU算法
- idea配置python和django开发环境
- Android集成Google Mobile Service(GMS)
- 验证码,按钮一分钟内不可点击。
- Qt的句柄类和实体类-QObject和QOjectPrivate
- Java设计模式—生产者消费者模式(阻塞队列实现)
- 进程的一些概念-父亲进程、子进程、僵尸进程、孤儿进程
- 安装mongodb
- HttpClient 4.5.2 学习随笔(1)