diff --git a/README.md b/README.md
index 5bdb4e1..a02d168 100644
--- a/README.md
+++ b/README.md
@@ -1,8 +1,6 @@
| 🍏 | 🍎 | 🍐 | 🍈 | 🥑 | 🥔| 🍠 | 🥝 | 🍱 | 🥞 |🌽| 🥦
| :--------: | :---------: | :---------: | :---------: | :---------: | :---------:| :---------: | :-------: | :-------:| :------:|:------:| :--------: |
| [JAVA基础](#JAVA基础) | [JVM知识](#JVM知识)|[开源框架知识](#开源框架知识) | [操作系统知识](#操作系统) |[多线程与并发](#多线程与并发)|[TCP与HTTP](#TCP与HTTP)| [架构设计与分布式](#架构设计与分布式) |[数据结构与算法](#数据结构与算法)|[数据库](#数据库知识)| [消息队列](#消息队列)|[缓存](#缓存) | [搜索](#搜索)
-### 求职、技术交流微信群
-
### JAVA基础
- [String,Stringbuffer,StringBuilder的区](http://www.cnblogs.com/su-feng/p/6659064.html)。
diff --git a/image/WechatIMG2430.jpeg b/image/WechatIMG2430.jpeg
new file mode 100644
index 0000000..297c61e
Binary files /dev/null and b/image/WechatIMG2430.jpeg differ
diff --git a/pom.xml b/pom.xml
index 4594050..5496700 100644
--- a/pom.xml
+++ b/pom.xml
@@ -20,12 +20,46 @@
cglib
2.2.2
-
+
+
+
+ org.testng
+ testng
+ 6.14.3
+
+
+
- org.apache.zookeeper
- zookeeper
- 3.4.6
+ com.google.guava
+ guava
+ 23.0
+
+
+ commons-io
+ commons-io
+ 2.4
+
+
+
+ org.projectlombok
+ lombok
+ 1.18.8
+ provided
+
+
+
+ joda-time
+ joda-time
+ 2.10.1
+
+
+
+ org.apache.commons
+ commons-lang3
+ 3.9
+
+
diff --git a/src/main/java/com/algorithm/study/demo/AbstractLoadBalance.java b/src/main/java/com/algorithm/study/demo/AbstractLoadBalance.java
deleted file mode 100644
index 14d40f1..0000000
--- a/src/main/java/com/algorithm/study/demo/AbstractLoadBalance.java
+++ /dev/null
@@ -1,15 +0,0 @@
-package com.algorithm.study.demo;
-
-/**
- * @Author: liuxun
- * @CreateDate: 2019/2/23 下午3:20
- * @Version: 1.0
- */
-public abstract class AbstractLoadBalance {
- public String select(String name){
- String newName="hello "+name;
- doSelect(newName);
- return newName;
- }
- protected abstract String doSelect(String name);
-}
diff --git a/src/main/java/com/algorithm/study/demo/LRUCache/LRUCache.java b/src/main/java/com/algorithm/study/demo/LRUCache/LRUCache.java
index 6234068..0fcbb70 100644
--- a/src/main/java/com/algorithm/study/demo/LRUCache/LRUCache.java
+++ b/src/main/java/com/algorithm/study/demo/LRUCache/LRUCache.java
@@ -1,48 +1,152 @@
package com.algorithm.study.demo.LRUCache;
-import java.util.*;
+import java.util.HashMap;
+import java.util.Map;
/**
- * LinkedHashMap实现LRU缓存
+ * LRU缓存链表实现思路
+ * 每次写入数据时将数据放入链表头结点。
+ * 使用数据时候将数据移动到头结点。
+ * 缓存数量超过阈值时移除链表尾部数据。
* @Author: liuxun
- * @CreateDate: 2019/2/13 上午10:24
+ * @CreateDate: 2018/7/12 下午6:05
* @Version: 1.0
*/
public class LRUCache {
- MapCache cache;
- public LRUCache(int capacity) {
- this.cache = new MapCache(capacity);
+ class Node{
+ private int key;
+ private int value;
+ private Node prev;
+ private Node next;
+
+ public Node(int key,int value){
+ this.key=key;
+ this.value=value;
+ }
+ public Node(){}
+
+ public int getKey() {
+ return key;
+ }
+
+ public void setKey(int key) {
+ this.key = key;
+ }
+
+ public int getValue() {
+ return value;
+ }
+
+ public void setValue(int value) {
+ this.value = value;
+ }
+ }
+ private void moveToHead(Node node){
+ remove(node);
+ addNode(node);
}
+ //删除尾节点
+ private Node popTail(){
+ Node prevNode= tail.prev;
+ tail.prev=prevNode.prev;
+ prevNode.prev.next=tail;
- public int get(int key) {
- return cache.getOrDefault(key, -1);
+ prevNode.next=null;
+ prevNode.prev=null;
+
+ size--;
+ return prevNode;
}
+ //删除中间节点
+ private void remove(Node node){
+ Node prevNode=node.prev;
+ Node nextNode=node.next;
- public void put(int key, int value) {
- cache.put(key, value);
+ prevNode.next=nextNode;
+ nextNode.prev=prevNode;
+
+ node.next=null;
+ node.prev=null;
+
+ size--;
}
- public Collection> getAll() {
- return new ArrayList>(cache.entrySet());
+ //添加节点
+ private void addNode(Node node){
+ node.next=head.next;
+ node.prev=head;
+ node.next.prev=node;
+ head.next=node;
+ size++;
+ }
+ private Map cache=new HashMap();
+ private int size=0;
+ private int capacity=0;
+ //头结点
+ private Node head;
+ //尾结点
+ private Node tail;
+ public LRUCache(int capacity) {
+ this.capacity=capacity;
+ //初始化头尾节点
+ this.head=new Node();
+ this.tail=new Node();
+ head.next=tail;
+ tail.prev=head;
}
- class MapCache extends LinkedHashMap {
- public int max;
- public MapCache(int max) {
- super(max, 0.75f, true);
- this.max = max;
+
+ public int get(int key) {
+ //从缓存获取
+ Node node=cache.get(key);
+ if(null==node){
+ return -1;
}
- protected boolean removeEldestEntry(Map.Entry eldest) {
- return size() > max;
+ //数据移到头结点
+ moveToHead(node);
+ return node.value;
+ }
+
+ public void put(int key, int value) {
+ Node node=cache.get(key);
+ if(null==node){
+ node=new Node(key,value);
+ //写入新节点至头节点
+ addNode(node);
+ cache.put(key,node);
+ //如果容量已满,删除尾节点
+ if(size>capacity){
+ //删除尾节点
+ Node delNode=popTail();
+ cache.remove(delNode.key);
+ }
+ }else{
+ //数据更新并移到头结点
+ node.value=value;
+ moveToHead(node);
}
+ }
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder() ;
+ for (Node node = head;node!=null;node=node.next){
+ sb.append(node.getKey()).append(":")
+ .append(node.getValue())
+ .append("-->");
+ }
+ return sb.toString();
}
public static void main(String[] args) {
- LRUCache map = new LRUCache(2) ;
- map.put(1,1);
- System.out.println(map.get(4));
- for (Map.Entry e : map.getAll()){
- System.out.print(e.getKey() + " : " + e.getValue() + "\t");
- }
+ LRUCache lruMap=new LRUCache(2);
+ lruMap.put(1,1);
+ lruMap.put(2,2);
+ lruMap.get(1);
+ lruMap.put(3,3);
+ lruMap.get(2);
+ lruMap.put(4,4);
+ lruMap.get(1);
+ lruMap.get(3);
+ lruMap.get(4);
+ System.out.println(lruMap.toString());
}
-}
-
+}
diff --git a/src/main/java/com/algorithm/study/demo/LRUCache/LRULinkedMap.java b/src/main/java/com/algorithm/study/demo/LRUCache/LRULinkedMap.java
index 18dc367..7f84245 100644
--- a/src/main/java/com/algorithm/study/demo/LRUCache/LRULinkedMap.java
+++ b/src/main/java/com/algorithm/study/demo/LRUCache/LRULinkedMap.java
@@ -1,7 +1,5 @@
package com.algorithm.study.demo.LRUCache;
-import java.util.ArrayList;
-import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -11,33 +9,25 @@
* @CreateDate: 2018/7/12 下午8:42
* @Version: 1.0
*/
-public class LRULinkedMap {
+public class LRULinkedMap extends LinkedHashMap {
/**
* 最大缓存大小
*/
- private int cacheSize;
- private LinkedHashMap cacheMap ;
- public LRULinkedMap(int cacheSize) {
- this.cacheSize = cacheSize;
- cacheMap = new LinkedHashMap(16,0.75F,true){
- @Override
- protected boolean removeEldestEntry(Map.Entry eldest) {
- if (cacheSize + 1 == cacheMap.size()){
- return true ;
- }else {
- return false ;
- }
- }
- };
+ private int CACHESIZE;
+ public LRULinkedMap(int cacheSize){
+ // true 表示让 linkedHashMap 按照访问顺序来进行排序,最近访问的放在头部,最老访问的放在尾部。
+ super(cacheSize,0.75f,true);
+ CACHESIZE=cacheSize;
}
- public void put(K key,V value){
- cacheMap.put(key,value) ;
- }
- public V get(K key){
- return cacheMap.get(key) ;
- }
- public Collection> getAll() {
- return new ArrayList>(cacheMap.entrySet());
+
+ /**
+ * 删除元素条件
+ * @param eldest
+ * @return
+ */
+ @Override
+ protected boolean removeEldestEntry(Map.Entry eldest){
+ return size()>CACHESIZE;
}
public static void main(String[] args) {
LRULinkedMap map = new LRULinkedMap(4) ;
@@ -47,7 +37,7 @@ public static void main(String[] args) {
map.put("4",4);
System.out.println(map.get("1"));
map.put("5",5);
- for (Map.Entry e : map.getAll()){
+ for (Map.Entry e : map.entrySet()){
System.out.print(e.getKey() + " : " + e.getValue() + "\t");
}
}
diff --git a/src/main/java/com/algorithm/study/demo/LRUCache/LRUMap.java b/src/main/java/com/algorithm/study/demo/LRUCache/LRUMap.java
deleted file mode 100644
index f0363a3..0000000
--- a/src/main/java/com/algorithm/study/demo/LRUCache/LRUMap.java
+++ /dev/null
@@ -1,168 +0,0 @@
-package com.algorithm.study.demo.LRUCache;
-
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * LRU缓存链表实现思路
- * 每次写入数据时将数据放入链表头结点。
- * 使用数据时候将数据移动到头结点。
- * 缓存数量超过阈值时移除链表尾部数据。
- * @Author: liuxun
- * @CreateDate: 2018/7/12 下午6:05
- * @Version: 1.0
- */
-public class LRUMap {
- private final Map cacheMap = new HashMap<>();
- /**
- * 最大缓存大小
- */
- private int cacheSize;
- /**
- * 节点大小
- */
- private int nodeCount;
- /**
- * 头结点
- */
- private Node header;
- /**
- * 尾结点
- */
- private Node tailer;
- public LRUMap(int cacheSize) {
- this.cacheSize = cacheSize;
- this.header=null;
- this.tailer=null;
- }
- public void put(K key, V value) {
- cacheMap.put(key, value);
- //双向链表中添加结点
- addNode(key, value);
- }
- public V get(K key){
- Node node = getNode(key);
- //移动到头结点
- moveToHead(node) ;
- return cacheMap.get(key);
- }
- private void moveToHead(Node node){
- //如果是最后的一个节点
- if (node.next == null){
- node.tail.next=null;
- tailer=node.tail;
- nodeCount -- ;
- }
- //如果是本来就是头节点 不作处理
- if (node.tail == null){
- return ;
- }
- //如果处于中间节点
- if (node.tail != null && node.next != null){
- //它的上一节点指向它的下一节点 也就删除当前节点
- node.tail.next=node.next;
- nodeCount -- ;
- }
- //最后在头部增加当前节点
- //注意这里需要重新 new 一个对象,不然原本的node 还有着下面的引用,会造成内存溢出。
- node = new Node<>(node.getKey(),node.getValue()) ;
- addHead(node) ;
- }
- /**
- * 链表查询 效率较低
- * @param key
- * @return
- */
- private Node getNode(K key){
- for (Node node = header;node!=null;node=node.next){
- if (node.getKey().equals(key)){
- return node ;
- }
- }
- return null ;
- }
- /**
- * 写入头结点
- * @param key
- * @param value
- */
- private void addNode(K key, V value) {
- Node node = new Node<>(key, value);
- //容量满了删除最后一个
- if (cacheSize == nodeCount) {
- //删除尾结点
- delTail();
- }
- //写入头结点
- addHead(node);
- }
- /**
- * 添加头结点
- *
- * @param node
- */
- private void addHead(Node node) {
- if (header==null){
- tailer=node;
- }else{
- header.tail=node;
- node.next=header;
- }
- header=node;
- nodeCount++;
- }
- private void delTail() {
- //把尾结点从缓存中删除
- cacheMap.remove(tailer.getKey());
- tailer.tail.next=null;
- tailer=tailer.tail;
- nodeCount--;
- }
- private class Node {
- private K key;
- private V value;
- Node tail;
- Node next;
- public Node(K key, V value) {
- this.key = key;
- this.value = value;
- }
- public Node() {
- }
- public K getKey() {
- return key;
- }
- public void setKey(K key) {
- this.key = key;
- }
- public V getValue() {
- return value;
- }
- public void setValue(V value) {
- this.value = value;
- }
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder() ;
- for (Node node = header;node!=null;node=node.next){
- if (node.getKey()!=null){
- sb.append(node.getKey()).append(":")
- .append(node.getValue())
- .append("-->");
- }
- }
- return sb.toString();
- }
- public static void main(String[] args) {
- LRUMap lruMap=new LRUMap<>(3);
- lruMap.put("1","1");
- lruMap.put("2","2");
- lruMap.put("3","3");
- lruMap.get("1");
- lruMap.put("4","4");
- System.out.println(lruMap.toString());
- System.out.println(lruMap.cacheSize);
- }
-
-}
diff --git a/src/main/java/com/algorithm/study/demo/MainTest.java b/src/main/java/com/algorithm/study/demo/MainTest.java
deleted file mode 100644
index 227faf2..0000000
--- a/src/main/java/com/algorithm/study/demo/MainTest.java
+++ /dev/null
@@ -1,32 +0,0 @@
-package com.algorithm.study.demo;
-
-import java.lang.ref.PhantomReference;
-import java.lang.ref.Reference;
-import java.lang.ref.ReferenceQueue;
-
-/**
- * @Author: liuxun
- * @CreateDate: 2019/1/2 上午11:29
- * @Version: 1.0
- */
-public class MainTest {
- public static void main(String[] args) {
-// Object counter = new Object();
-// ReferenceQueue refQueue = new ReferenceQueue<>();
-// PhantomReference