public class Node<K, T> {
K key;
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
T value;
/**
* 下一个节点
*/
Node<K, T> next;
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public Node() {
}
public Node(K key, T value) {
this.key = key;
this.value = value;
}
public Node<K, T> getNext() {
return next;
}
public void setNext(Node<K, T> next) {
this.next = next;
}
}
节点
public class LruLinkMap<K, T> {
/**
* 头节点
*/
Node<K, T> mHeader;
private int mNumber;
private int currentNumber = 0;
public LruLinkMap(int number) {
mNumber = number;
// 初始化头节点
mHeader = new Node<>();
}
public Node<K, T> getNode(K key) {
Node<K, T> node = mHeader;
while (node.next != null) {
Node<K, T> p = node.next;
if (p.getKey().equals(key)) {
return p;
}
node = p;
}
return null;
}
public void insertNode(Node<K, T> node) {
// 首先先判断是否在队列中,如果在队列中,就把队列中已经有的参数删除,然后将已有的插入队首
// 如果不在队列中,就先判断当前的大小是否已经达到容量,如果没有插入到队首
// 如果达到容量,就删除队尾,将新元素插入队首
if (node == null) {
return;
}
boolean isFull = (currentNumber == mNumber);
// 从头节点开始便利
Node<K, T> p = mHeader;
while (p.next != null) {
Node<K, T> currentNode = p.next;
if (currentNode.getKey().equals(node.getKey())) {
// 找到当前的节点是已经存在的节点,删除当前节点
Log.d("zpb", "缓存已经满了,删除已经存在的节点{" + currentNode.getKey() + ":" + currentNode.getValue() + "}");
p.next = currentNode.next;
currentNumber--;
break;
} else if (isFull && currentNode.next == null) {
Log.d("zpb", "缓存已经满了,删除尾节点{" + currentNode.getKey() + ":" + currentNode.getValue() + "}");
// 如果当前是尾节点
p.next = currentNode.next;
// 删除当前节点
currentNumber--;
break;
}
p = currentNode;
}
// 然后将节点插入头节点
Node<K, T> next = mHeader.next;
mHeader.next = node;
node.next = next;
currentNumber++;
}
@Override
public String toString() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("{");
Node<K, T> node = mHeader;
while (node.next != null) {
Node<K, T> p = node.next;
stringBuffer.append(p.getKey() + ":" + p.getValue() + ",");
node = p;
}
stringBuffer.append("}");
return stringBuffer.toString();
}
}
缓存map
package com.example.meitu.data;
import android.util.Log;
/**
* Created by meitu on 2019/1/21.
*/
public class LRUCache<K, T> {
private int maxNumber = 100;
private LruLinkMap<K, T> lruCache;
public LRUCache(int maxNumber) {
this.maxNumber = maxNumber;
lruCache = new LruLinkMap<>(maxNumber);
}
public void put(Node<K, T> node) {
lruCache.insertNode(node);
}
public Node<K,T> get(K key) {
return lruCache.getNode(key);
}
public void print(){
Log.d("zpb","cache:"+lruCache.toString());
}
}
缓存类
public class MainActivity extends AppCompatActivity {
LRUCache<String, String> mCache;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCache = new LRUCache<>(5);
test();
}
public void test() {
Log.d("zpb", "添加1");
mCache.put(new Node<String, String>("1", "noe"));
mCache.print();
Log.d("zpb", "添加2");
mCache.put(new Node<String, String>("2", "two"));
mCache.print();
Log.d("zpb", "添加3");
mCache.put(new Node<String, String>("3", "three"));
mCache.print();
Log.d("zpb", "添加4");
mCache.put(new Node<String, String>("4", "four"));
mCache.print();
mCache.print();
Log.d("zpb", "此处替换");
Log.d("zpb","2="+mCache.get("2").getValue());
mCache.put(new Node<String, String>("2", "no for"));
Log.d("zpb","2="+mCache.get("2").getValue());
mCache.print();
mCache.put(new Node<String, String>("5", "five"));
mCache.print();
Log.d("zpb", "此处满了");
mCache.put(new Node<String, String>("6", "six"));
mCache.print();
}
}
测试类