Skip to content

Commit 5e466dd

Browse files
committed
+
1 parent a0d90d9 commit 5e466dd

File tree

4 files changed

+173
-1
lines changed

4 files changed

+173
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Java-interview-coding
22

33
#### 初衷
4-
近几年算起来也面试了不少人(至少三位数),有初出茅庐的小伙子也有工作10年+的资深架构师,遗憾的是真正对java本身得心应手的少之又少,算的上精通的更是聊胜于无。大部分都是知其然不知所以然(也许那些精通java的都去大厂面试了吧)。于是乎,我按我近年来的java笔记整理了一份面向编码的面试题库,也算是多年心血物尽所用,希望对新鸟老鸟有所帮助
4+
整理一份java编码版面试题,一当笔记二分享给有需要的人
55

66
#### 目录
77
- 集合

src/io/vakin/algorithm/LRU.java

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package io.vakin.algorithm;
2+
3+
import java.util.HashMap;
4+
import java.util.Iterator;
5+
6+
public class LRU<K, V> implements Iterable<K> {
7+
8+
private Node head;
9+
private Node tail;
10+
private HashMap<K, Node> map;
11+
private int maxSize;
12+
13+
private class Node {
14+
15+
Node pre;
16+
Node next;
17+
K k;
18+
V v;
19+
20+
public Node(K k, V v) {
21+
this.k = k;
22+
this.v = v;
23+
}
24+
}
25+
26+
public LRU(int maxSize) {
27+
28+
this.maxSize = maxSize;
29+
this.map = new HashMap<>(maxSize * 4 / 3);
30+
}
31+
32+
public V get(K key) {
33+
34+
if (!map.containsKey(key)) {
35+
return null;
36+
}
37+
38+
Node node = map.get(key);
39+
unlink(node);
40+
appendHead(node);
41+
42+
return node.v;
43+
}
44+
45+
public void put(K key, V value) {
46+
47+
if(head == null){
48+
head = new Node(key, value);
49+
tail = head;
50+
return;
51+
}
52+
if (map.containsKey(key)) {
53+
Node node = map.get(key);
54+
unlink(node);
55+
}
56+
57+
Node node = new Node(key, value);
58+
map.put(key, node);
59+
appendHead(node);
60+
61+
if (map.size() > maxSize) {
62+
Node toRemove = removeTail();
63+
map.remove(toRemove);
64+
}
65+
}
66+
67+
private void unlink(Node node) {
68+
Node pre = node.pre;
69+
Node next = node.next;
70+
if(pre == null){
71+
}else {
72+
pre.next = next;
73+
next.pre = pre;
74+
}
75+
}
76+
77+
private void appendHead(Node node) {
78+
node.next = head;
79+
head.pre = node;
80+
}
81+
82+
private Node removeTail() {
83+
Node node = tail;
84+
if(node == null)return null;
85+
tail = tail.pre;
86+
return node;
87+
}
88+
89+
@Override
90+
public Iterator<K> iterator() {
91+
92+
return new Iterator<K>() {
93+
94+
private Node cur = head.next;
95+
96+
@Override
97+
public boolean hasNext() {
98+
return cur != tail;
99+
}
100+
101+
@Override
102+
public K next() {
103+
Node node = cur;
104+
cur = cur.next;
105+
return node.k;
106+
}
107+
};
108+
}
109+
110+
public static void main(String[] args) {
111+
LRU<Object, Object> lru = new LRU<>(10);
112+
for (int i = 0; i < 5; i++) {
113+
lru.put(i, i);
114+
}
115+
116+
lru.get(3);
117+
lru.get(2);
118+
119+
Iterator<Object> iterator = lru.iterator();
120+
while(iterator.hasNext()){
121+
System.out.println(iterator.next());
122+
}
123+
124+
}
125+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.vakin.concurrent;
2+
3+
import java.util.concurrent.atomic.AtomicInteger;
4+
5+
/**
6+
* 并发下计数器
7+
*/
8+
public class ConcurrentCounter {
9+
10+
AtomicInteger count = new AtomicInteger(0);
11+
12+
public int add(){
13+
return count.incrementAndGet();
14+
}
15+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.vakin.concurrent;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
5+
6+
/**
7+
*
8+
* @description <br>
9+
* <p>需求:解析一个Excel里多个sheet的数据时,使用多线程每个线程解析一个sheet里的数据,等到所有的sheet都解析完之后,程序需要提示解析完成。</p>
10+
* @author <a href="mailto:vakinge@gmail.com">vakin</a>
11+
* @date 2018年8月7日
12+
*/
13+
public class CountDownLatchTest {
14+
15+
static CountDownLatch c = new CountDownLatch(2);
16+
17+
public static void main(String[] args) throws InterruptedException {
18+
new Thread(new Runnable() {
19+
@Override
20+
public void run() {
21+
System.out.println("解析中..");
22+
c.countDown();
23+
System.out.println("解析完成");
24+
c.countDown();
25+
}
26+
}).start();
27+
28+
//阻塞等到两个子任务完成
29+
c.await();
30+
System.out.println("全部解析完成");
31+
}
32+
}

0 commit comments

Comments
 (0)