Skip to content

Commit d02ce84

Browse files
committed
✨ Introducing new features. base on LinkedHashMap
1 parent 73de2d0 commit d02ce84

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.crossoverjie.actual;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.LinkedHashMap;
6+
import java.util.Map;
7+
8+
/**
9+
* Function:
10+
*
11+
* @author crossoverJie
12+
* Date: 05/04/2018 12:04
13+
* @since JDK 1.8
14+
*/
15+
public class LRULinkedMap<K,V> {
16+
17+
18+
/**
19+
* 最大缓存大小
20+
*/
21+
private int cacheSize;
22+
23+
private LinkedHashMap<K,V> cacheMap ;
24+
25+
26+
public LRULinkedMap(int cacheSize) {
27+
this.cacheSize = cacheSize;
28+
29+
cacheMap = new LinkedHashMap(16,0.75F,true){
30+
@Override
31+
protected boolean removeEldestEntry(Map.Entry eldest) {
32+
if (cacheSize + 1 == cacheMap.size()){
33+
return true ;
34+
}else {
35+
return false ;
36+
}
37+
}
38+
};
39+
}
40+
41+
public void put(K key,V value){
42+
cacheMap.put(key,value) ;
43+
}
44+
45+
public V get(K key){
46+
return cacheMap.get(key) ;
47+
}
48+
49+
50+
public Collection<Map.Entry<K, V>> getAll() {
51+
return new ArrayList<Map.Entry<K, V>>(cacheMap.entrySet());
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.crossoverjie.actual;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Map;
6+
7+
import static org.junit.Assert.*;
8+
9+
public class LRULinkedMapTest {
10+
@Test
11+
public void put() throws Exception {
12+
LRULinkedMap<String,Integer> map = new LRULinkedMap(3) ;
13+
map.put("1",1);
14+
map.put("2",2);
15+
map.put("3",3);
16+
17+
for (Map.Entry<String, Integer> e : map.getAll()){
18+
System.out.print(e.getKey() + " : " + e.getValue() + "\t");
19+
}
20+
21+
System.out.println("");
22+
map.put("4",4);
23+
for (Map.Entry<String, Integer> e : map.getAll()){
24+
System.out.print(e.getKey() + " : " + e.getValue() + "\t");
25+
}
26+
}
27+
28+
@Test
29+
public void put2() throws Exception {
30+
LRULinkedMap<String,Integer> map = new LRULinkedMap(4) ;
31+
map.put("1",1);
32+
map.put("2",2);
33+
map.put("3",3);
34+
map.put("4",4);
35+
36+
for (Map.Entry<String, Integer> e : map.getAll()){
37+
System.out.print(e.getKey() + " : " + e.getValue() + "\t");
38+
}
39+
40+
System.out.println("");
41+
map.put("5",5);
42+
for (Map.Entry<String, Integer> e : map.getAll()){
43+
System.out.print(e.getKey() + " : " + e.getValue() + "\t");
44+
}
45+
}
46+
@Test
47+
public void get() throws Exception {
48+
LRULinkedMap<String,Integer> map = new LRULinkedMap(4) ;
49+
map.put("1",1);
50+
map.put("2",2);
51+
map.put("3",3);
52+
map.put("4",4);
53+
54+
for (Map.Entry<String, Integer> e : map.getAll()){
55+
System.out.print(e.getKey() + " : " + e.getValue() + "\t");
56+
}
57+
58+
System.out.println("");
59+
map.get("1") ;
60+
for (Map.Entry<String, Integer> e : map.getAll()){
61+
System.out.print(e.getKey() + " : " + e.getValue() + "\t");
62+
}
63+
}
64+
65+
}

0 commit comments

Comments
 (0)