From 275fafd4efe22853fedd0dd5814640ce768dc2b8 Mon Sep 17 00:00:00 2001 From: myself Date: Thu, 7 Mar 2019 18:34:16 +0800 Subject: [PATCH] put and get bug fix --- .../crossoverjie/actual/LRUAbstractMap.java | 30 +++++++++---------- .../crossoverjie/actual/AbstractMapTest.java | 30 +++++++++++++++++++ 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/crossoverjie/actual/LRUAbstractMap.java b/src/main/java/com/crossoverjie/actual/LRUAbstractMap.java index 04157337..10ef2860 100644 --- a/src/main/java/com/crossoverjie/actual/LRUAbstractMap.java +++ b/src/main/java/com/crossoverjie/actual/LRUAbstractMap.java @@ -119,31 +119,29 @@ public Object put(Object key, Object value) { }else { Node cNode = currentNode ; Node nNode = cNode ; - + Node pNode = cNode; //存在就覆盖 - if (nNode.key == key){ - cNode.val = value ; - } - while (nNode.next != null){ + while (nNode != null){ //key 存在 就覆盖 简单判断 if (nNode.key == key){ nNode.val = value ; - break ; + return null ; }else { - //不存在就新增链表 - sizeUp(); - Node node = new Node(nNode,null,key,value) ; - //写入队列 - QUEUE.offer(currentNode) ; - - cNode.next = node ; } - + pNode = nNode; nNode = nNode.next ; } + //不存在就新增节点 + sizeUp(); + Node node = new Node(pNode,null,key,value) ; + pNode.next = node; + //写入队列 + QUEUE.offer(currentNode) ; + + } return null ; @@ -171,7 +169,7 @@ public Object get(Object key) { } Node nNode = currentNode ; - while (nNode.next != null){ + while (nNode != null){ if (nNode.key == key){ @@ -184,7 +182,7 @@ public Object get(Object key) { nNode = nNode.next ; } - return super.get(key); + return null; } diff --git a/src/test/java/com/crossoverjie/actual/AbstractMapTest.java b/src/test/java/com/crossoverjie/actual/AbstractMapTest.java index a384d5b9..85790bb2 100644 --- a/src/test/java/com/crossoverjie/actual/AbstractMapTest.java +++ b/src/test/java/com/crossoverjie/actual/AbstractMapTest.java @@ -4,10 +4,20 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + public class AbstractMapTest { private final static Logger LOGGER = LoggerFactory.getLogger(AbstractMapTest.class); + //hash值相同的字符串 + private final static String s1 = "CE_SxKD?V2ELWeRhnv`bD5Jpiq>gV7lpx9ZpPV?Qfo86afNblg>[w=JuJXrrXhoW"; + private final static String s2 = "_T`KhoXXuEw?T]Q`YqbsPuC7]sh@kO]VD@3nA:Z9rYWdPAMUa@DzRh?:n[^iko^\\"; + private final static String s3 = "vyqHAKlFv89cGZUj sList = Arrays.asList(s1, s2, s3, s4); @Test public void test(){ LRUAbstractMap map = new LRUAbstractMap() ; @@ -20,6 +30,26 @@ public void test(){ map.remove(1) ; LOGGER.info("getSize"+map.size()); } + @Test + public void testPut(){ + LRUAbstractMap map = new LRUAbstractMap() ; + int count = 1; + for (String s : sList) { + System.out.println(s); + map.put(s, count++); + } + for (String s : sList) { + System.out.println(map.get(s)); + } + for (String s : sList) { + System.out.println(s); + map.put(s, count++); + } + for (String s : sList) { + System.out.println(map.get(s)); + } + + } public static void main(String[] args) { LRUAbstractMap map = new LRUAbstractMap() ;