99 * @CreateDate: 2019/1/23 下午4:03
1010 * @Version: 1.0
1111 */
12- public class LRULinked < K , V > {
12+ public class LRULinked {
1313 //缓存
14- private final Map <K , V > cacheMap = new HashMap <>();
14+ private final Map <Integer , Integer > cacheMap = new HashMap <>();
1515 //根节点
16- private Node < K , V > root ;
16+ private Node root ;
1717 private int cacheSize ;
1818 private int size ;
1919 public LRULinked (int cacheSize ){
@@ -24,14 +24,15 @@ public LRULinked(int cacheSize){
2424 * 插入头结点
2525 * @param value
2626 */
27- public void put (K key ,V value ){
28- Node <K , V > node =new Node <K , V >(key ,value );
27+ public void put (int key ,int value ){
28+ cacheMap .put (key ,value );
29+ Node node =new Node (key ,value );
2930 if (size ==cacheSize ){//容量满了删除尾节点
30- Node < K , V > temp =root .next ;
31+ Node temp =root .next ;
3132 if (temp ==null ){
3233 root =null ;
3334 }else {
34- Node < K , V > current =root ;
35+ Node current =root ;
3536 while (temp .next !=null ){
3637 current =temp ;
3738 temp =temp .next ;
@@ -44,10 +45,13 @@ public void put(K key,V value){
4445 root =node ;
4546 size ++;
4647 }
47- public V get (K key ){
48- for (Node <K ,V > node = root ; node !=null &&!root .key .equals (key ); node =node .next ){
48+ public int get (int key ){
49+ if (!cacheMap .containsKey (key )){
50+ return -1 ;
51+ }
52+ for (Node node = root ; node !=null &&!root .key .equals (key ); node =node .next ){
4953 if (node .next .key .equals (key )){
50- Node < K , V > nodeNew =new Node < K , V > (node .next .key ,node .next .value );
54+ Node nodeNew =new Node (node .next .key ,node .next .value );
5155 node .next =node .next .next ;
5256 size --;
5357 this .put (nodeNew .key ,nodeNew .value );//查找的节点放到头结点
@@ -60,7 +64,7 @@ public V get(K key){
6064 public String toString (){
6165 StringBuilder sb =new StringBuilder ();
6266 int j =0 ;
63- Node < K , V > temp =root ;
67+ Node temp =root ;
6468 while (j <size ){
6569 sb .append (temp .value );
6670 temp = temp .next ;//找到最后一个结点
@@ -69,26 +73,24 @@ public String toString(){
6973 return sb .toString ();
7074 }
7175
72- public static class Node < K , V > {
73- private K key ;
74- private V value ;
75- private Node < K , V > next ;
76- public Node (K key , V value ){
76+ public static class Node {
77+ private Integer key ;
78+ private Integer value ;
79+ private Node next ;
80+ public Node (Integer key , Integer value ){
7781 this .key =key ;
7882 this .value =value ;
7983 }
8084 }
8185
8286 public static void main (String [] args ) {
83- LRULinked < String , String > linked =new LRULinked <> (3 );
84- linked .put ("a" , "a" );
85- linked .put ("b" , "b" );
86- linked .put ("c" , "c" );
87- linked .get ("b" );
88- linked .put ("d" , "d" );
87+ LRULinked linked =new LRULinked (3 );
88+ linked .put (1 , 2 );
89+ linked .put (2 , 2 );
90+ linked .put (3 , 3 );
91+ System . out . println ( linked .get (1 ) );
92+ linked .put (4 , 4 );
8993 System .out .println (linked .size );
9094 System .out .println (linked .toString ());
91- System .out .println (linked .toString ());
92- System .out .println (linked .size );
9395 }
9496}
0 commit comments