Skip to content

Commit d289dad

Browse files
committed
JAVA 知识体系-LRU实现
1 parent e67e6f5 commit d289dad

2 files changed

Lines changed: 3 additions & 6 deletions

File tree

src/main/java/com/algorithm/study/demo/LRUCache/LRULinkedMap.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public static void main(String[] args) {
4545
map.put("2",2);
4646
map.put("3",3);
4747
map.put("4",4);
48+
System.out.println(map.get("1"));
4849
map.put("5",5);
4950
for (Map.Entry<String, Integer> e : map.getAll()){
5051
System.out.print(e.getKey() + " : " + e.getValue() + "\t");

src/main/java/com/algorithm/study/demo/LRUCache/LRUMap.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,10 @@ private void moveToHead(Node<K,V> node){
7474
* @return
7575
*/
7676
private Node<K,V> getNode(K key){
77-
Node<K,V> node = header ;
78-
while (node != null){
77+
for (Node<K,V> node = header;node!=null;node=node.next){
7978
if (node.getKey().equals(key)){
8079
return node ;
8180
}
82-
node = node.next ;
8381
}
8482
return null ;
8583
}
@@ -147,14 +145,12 @@ public void setValue(V value) {
147145
@Override
148146
public String toString() {
149147
StringBuilder sb = new StringBuilder() ;
150-
Node<K,V> node = header ;
151-
while (node != null){
148+
for (Node<K,V> node = header;node!=null;node=node.next){
152149
if (node.getKey()!=null){
153150
sb.append(node.getKey()).append(":")
154151
.append(node.getValue())
155152
.append("-->");
156153
}
157-
node = node.next ;
158154
}
159155
return sb.toString();
160156
}

0 commit comments

Comments
 (0)