Skip to content

Commit 57e56f2

Browse files
committed
小傅哥,Java 面经手册 • 拿大厂Offer
1 parent 95501d9 commit 57e56f2

File tree

225 files changed

+5141
-39
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

225 files changed

+5141
-39
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/interview.iml
1+
/*.iml
22
/interview-04/interview-04.iml
33
/interview-07/interview-07.iml
44
/interview-05/interview-05.iml

README.md

Lines changed: 119 additions & 29 deletions
Large diffs are not rendered by default.

interview-09/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>interview</artifactId>
7+
<groupId>org.itstack</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>interview-09</artifactId>
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>org.apache.maven.plugins</groupId>
17+
<artifactId>maven-compiler-plugin</artifactId>
18+
<configuration>
19+
<source>7</source>
20+
<target>7</target>
21+
</configuration>
22+
</plugin>
23+
</plugins>
24+
</build>
25+
26+
27+
</project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.itstack.interview;
2+
3+
import java.util.concurrent.Delayed;
4+
import java.util.concurrent.TimeUnit;
5+
6+
public class TestDelayed implements Delayed {
7+
8+
private String str;
9+
private long time;
10+
11+
public TestDelayed(String str, long time, TimeUnit unit) {
12+
this.str = str;
13+
this.time = System.currentTimeMillis() + (time > 0 ? unit.toMillis(time) : 0);
14+
}
15+
16+
@Override
17+
public long getDelay(TimeUnit unit) {
18+
return time - System.currentTimeMillis();
19+
}
20+
21+
@Override
22+
public int compareTo(Delayed o) {
23+
TestDelayed work = (TestDelayed) o;
24+
long diff = this.time - work.time;
25+
if (diff <= 0) {
26+
return -1;
27+
} else {
28+
return 1;
29+
}
30+
}
31+
32+
public String getStr() {
33+
return str;
34+
}
35+
}
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
package org.itstack.interview.test;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import org.itstack.interview.TestDelayed;
5+
import org.junit.Test;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import sun.awt.util.IdentityLinkedList;
9+
10+
import java.util.*;
11+
import java.util.concurrent.*;
12+
13+
public class ApiTest {
14+
15+
private Logger logger = LoggerFactory.getLogger(ApiTest.class);
16+
17+
@Test
18+
public void test_BlockingQueue() {
19+
Queue<String> queue = new LinkedBlockingQueue<String>();
20+
}
21+
22+
@Test
23+
public void test_Queue() throws InterruptedException {
24+
Queue<String> queue00 = new LinkedList<String>();
25+
queue00.poll();
26+
queue00.offer("");
27+
28+
Queue<String> queue01 = new LinkedBlockingQueue<String>();
29+
queue01.offer("");
30+
queue01.poll();
31+
32+
Queue<String> queue02 = new ArrayBlockingQueue<String>(10);
33+
34+
}
35+
36+
@Test
37+
public void test_Deque_LinkedList(){
38+
Deque<String> deque = new LinkedList<>();
39+
deque.push("a");
40+
deque.push("b");
41+
deque.push("c");
42+
deque.push("d");
43+
44+
deque.offerLast("e");
45+
deque.offerLast("f");
46+
deque.offerLast("g");
47+
deque.offerLast("h");
48+
49+
deque.push("i");
50+
deque.offerLast("j");
51+
52+
System.out.println("数据出栈:");
53+
while (!deque.isEmpty()) {
54+
System.out.print(deque.pop() + " ");
55+
}
56+
57+
}
58+
59+
@Test
60+
public void test_DelayQueue() throws InterruptedException {
61+
DelayQueue<TestDelayed> delayQueue = new DelayQueue<TestDelayed>();
62+
delayQueue.offer(new TestDelayed("aaa", 5, TimeUnit.SECONDS));
63+
delayQueue.offer(new TestDelayed("ccc", 1, TimeUnit.SECONDS));
64+
delayQueue.offer(new TestDelayed("bbb", 3, TimeUnit.SECONDS));
65+
66+
logger.info(((TestDelayed) delayQueue.take()).getStr());
67+
logger.info(((TestDelayed) delayQueue.take()).getStr());
68+
logger.info(((TestDelayed) delayQueue.take()).getStr());
69+
}
70+
71+
@Test
72+
public void test_deque() {
73+
Deque<String> deque00 = new ArrayDeque<String>(10);
74+
deque00.offer("");
75+
deque00.poll();
76+
77+
deque00.push("");
78+
79+
Deque<String> deque01 = new LinkedList<String>();
80+
81+
Deque<String> deque02 = new LinkedBlockingDeque<String>();
82+
83+
Deque<String> deque04 = new ConcurrentLinkedDeque<String>();
84+
85+
}
86+
87+
@Test
88+
public void test_ArrayDeque2() {
89+
Deque<String> deque = new ArrayDeque<String>(1);
90+
91+
deque.push("a");
92+
deque.push("b");
93+
deque.push("c");
94+
deque.push("d");
95+
96+
deque.push("e");
97+
deque.push("f");
98+
99+
deque.offerLast("g");
100+
deque.offerLast("h");
101+
deque.offerLast("i");
102+
deque.offerLast("j");
103+
104+
System.out.println(deque.pop());
105+
System.out.println(deque.pop());
106+
System.out.println(deque.pop());
107+
System.out.println(deque.pop());
108+
109+
System.out.println(deque.pollLast());
110+
111+
}
112+
113+
114+
@Test
115+
public void test_ArrayDeque() {
116+
Deque<String> deque = new ArrayDeque<String>(1);
117+
118+
deque.push("a");
119+
deque.push("b");
120+
deque.push("c");
121+
deque.push("d");
122+
123+
deque.offerLast("e");
124+
deque.offerLast("f");
125+
deque.offerLast("g");
126+
deque.offerLast("h"); // 这时候扩容了
127+
128+
deque.push("i");
129+
deque.offerLast("j");
130+
131+
System.out.println("数据出栈:");
132+
while (!deque.isEmpty()) {
133+
System.out.print(deque.pop() + " ");
134+
}
135+
136+
}
137+
138+
@Test
139+
public void test_arraycopy() {
140+
int head = 0, tail = 0;
141+
142+
Object[] elements = new Object[8];
143+
elements[head = (head - 1) & (elements.length - 1)] = "a";
144+
elements[head = (head - 1) & (elements.length - 1)] = "b";
145+
elements[head = (head - 1) & (elements.length - 1)] = "c";
146+
elements[head = (head - 1) & (elements.length - 1)] = "d";
147+
148+
elements[tail] = "e";
149+
tail = (tail + 1) & (elements.length - 1);
150+
elements[tail] = "f";
151+
tail = (tail + 1) & (elements.length - 1);
152+
elements[tail] = "g";
153+
tail = (tail + 1) & (elements.length - 1);
154+
elements[tail] = "h";
155+
tail = (tail + 1) & (elements.length - 1);
156+
157+
System.out.println("head:" + head);
158+
System.out.println("tail:" + tail);
159+
160+
int p = head;
161+
int n = elements.length;
162+
int r = n - p; // number of elements to the right of p
163+
164+
System.out.println(JSON.toJSONString(elements));
165+
// head == tail 扩容
166+
Object[] a = new Object[8 << 1];
167+
System.arraycopy(elements, p, a, 0, r);
168+
System.out.println(JSON.toJSONString(a));
169+
System.arraycopy(elements, 0, a, r, p);
170+
System.out.println(JSON.toJSONString(a));
171+
elements = a;
172+
head = 0;
173+
tail = n;
174+
175+
a[head = (head - 1) & (a.length - 1)] = "i";
176+
elements[tail] = "j";
177+
tail = (tail + 1) & (elements.length - 1);
178+
179+
System.out.println(JSON.toJSONString(a));
180+
}
181+
182+
@Test
183+
public void test_stack() {
184+
Stack<String> s = new Stack<String>();
185+
186+
s.push("aaa");
187+
s.push("bbb");
188+
s.push("ccc");
189+
190+
System.out.println("获取最后一个元素:" + s.peek());
191+
System.out.println("获取最后一个元素:" + s.lastElement());
192+
System.out.println("获取最先放置元素:" + s.firstElement());
193+
194+
System.out.println("弹出一个元素[LIFO]:" + s.pop());
195+
System.out.println("弹出一个元素[LIFO]:" + s.pop());
196+
System.out.println("弹出一个元素[LIFO]:" + s.pop());
197+
198+
/**
199+
* 获取最后一个元素:ccc
200+
* 获取最后一个元素:ccc
201+
* 获取最先放置元素:aaa
202+
* 弹出一个元素[LIFO]:ccc
203+
* 弹出一个元素[LIFO]:bbb
204+
* 弹出一个元素[LIFO]:aaa
205+
*/
206+
}
207+
208+
}
Binary file not shown.
Binary file not shown.

interview-10/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>interview</artifactId>
7+
<groupId>org.itstack</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>interview-10</artifactId>
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>org.apache.maven.plugins</groupId>
17+
<artifactId>maven-compiler-plugin</artifactId>
18+
<configuration>
19+
<source>6</source>
20+
<target>6</target>
21+
</configuration>
22+
</plugin>
23+
</plugins>
24+
</build>
25+
26+
27+
</project>

0 commit comments

Comments
 (0)