Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LRUAbstractMap的put和get方法bug fix #147

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions src/main/java/com/crossoverjie/actual/LRUAbstractMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;
Expand Down Expand Up @@ -171,7 +169,7 @@ public Object get(Object key) {
}

Node nNode = currentNode ;
while (nNode.next != null){
while (nNode != null){

if (nNode.key == key){

Expand All @@ -184,7 +182,7 @@ public Object get(Object key) {
nNode = nNode.next ;
}

return super.get(key);
return null;
}


Expand Down
30 changes: 30 additions & 0 deletions src/test/java/com/crossoverjie/actual/AbstractMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<dC=:ySwDIaNANYeC9^ZD<W7usF[:A\\cMXsX;[MVw\\X`j^le";
private final static String s4 = ";nJAgZ32<69g\\o6O^<UjhbJRCN]AD`WDxhn[orjAFrQSo5ArQMf;RfKf@\\odVphd";
private final static List<String> sList = Arrays.asList(s1, s2, s3, s4);
@Test
public void test(){
LRUAbstractMap map = new LRUAbstractMap() ;
Expand All @@ -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() ;
Expand Down