This commit is contained in:
xiongraorao 2018-09-10 12:03:30 +08:00
parent 27490a9869
commit 427578c8a2
4 changed files with 38 additions and 12 deletions

View File

@ -1,10 +1,6 @@
package com.raorao;
<<<<<<< HEAD
import java.util.Scanner;
=======
import java.util.HashMap;
>>>>>>> 1c2b8c5ebb8faea5298e53672bf08b78be1c7c89
/**
* .
@ -17,15 +13,8 @@ public class Test {
private int a;
public static void main(String[] args) {
<<<<<<< HEAD
String[] s = new String[10];
System.out.println(s.length);
System.out.println(s[0]);
Scanner scanner = new Scanner(System.in);
=======
>>>>>>> 1c2b8c5ebb8faea5298e53672bf08b78be1c7c89
}
}

View File

@ -0,0 +1,36 @@
package com.raorao.java.althorithm;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* 缓存算法的实现 .
*
* LinkedHashMap的一个构造函数当参数accessOrder为true时即会按照访问顺序排序最近访问的放在最前最早访问的放在后面
*
* @author Xiong Raorao
* @since 2018-09-10-11:53
*/
public class LRU<K, V> extends LinkedHashMap<K, V> {
private final int MAX_CACHE_SIZE;
public LRU(int cacheSize) {
super((int) Math.ceil(cacheSize / 0.75) + 1, 0.75f, true);
MAX_CACHE_SIZE = cacheSize;
}
@Override
protected boolean removeEldestEntry(Map.Entry eldest) {
return size() > MAX_CACHE_SIZE;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (Map.Entry<K, V> entry : entrySet()) {
sb.append(String.format("%s:%s ", entry.getKey(), entry.getValue()));
}
return sb.toString();
}
}

View File

@ -14,7 +14,6 @@ public class BreakAgent {
public static void main(String[] args) throws ClassNotFoundException {
TreeSet
}
public static class Object {

View File

@ -26,3 +26,5 @@
# LRU实现
最小访问的算法实现
- [LRU缓存实现(Java)](https://www.cnblogs.com/lzrabbit/p/3734850.html)