Skip to content

Commit 2da83e5

Browse files
committed
+
1 parent 4855d41 commit 2da83e5

File tree

8 files changed

+137
-13
lines changed

8 files changed

+137
-13
lines changed

README.md

+23-11
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,26 @@
44
java基础编码版面试题。
55

66
#### 目录
7-
- 集合
8-
- io
9-
- bio
10-
- nio
11-
- aio
12-
- 本地io
13-
- 并发包
14-
- java8
15-
- 算法
16-
- 排序算法
17-
- 经典算法题
7+
* 基础
8+
* [集合](docs/collection.md)
9+
* [文件](docs/file.md)
10+
* [网络I/O](docs/IO.md)
11+
* [设计模式](docs/design-pattern.md)
12+
* 多线程
13+
* [线程池](docs/concurrent-threadpool.md)
14+
* [](docs/concurrent-lock.md)
15+
* [队列](docs/concurrent-queue.md)
16+
* [安全集合](docs/concurrent-collection.md)
17+
* [工具](docs/concurrent-tool.md)
18+
* Java新特性
19+
* [java 8](docs/java8.md)
20+
* [java 9](docs/java9.md)
21+
* [java 10](docs/java10.md)
22+
* [java 11](docs/java11.md)
23+
* 算法
24+
* [排序算法](docs/algorithm-sort.md)
25+
* [加密算法](docs/algorithm-crypt.md)
26+
* [其他算法](docs/algorithm-other.md)
27+
* 其他
28+
* [正则表达式](docs/regex.md)
29+
* [最佳实践](docs/best-practices.md)

docs/collection.md

Whitespace-only changes.

src/io/vakin/base/CollectionForeachRemoveElement.java renamed to src/io/vakin/collection/CollectionForeachRemoveElement.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.vakin.base;
16+
package io.vakin.collection;
1717

1818
/**
1919
* @description <br>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.vakin.concurrent;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
import java.util.concurrent.ExecutionException;
5+
6+
/**
7+
* @author Benjamin Winterberg
8+
*/
9+
public class CompletableFuture1 {
10+
11+
public static void main(String[] args) throws ExecutionException, InterruptedException {
12+
CompletableFuture<String> future = new CompletableFuture<>();
13+
14+
future.complete("42");
15+
16+
future
17+
.thenAccept(System.out::println)
18+
.thenAccept(v -> System.out.println("done"));
19+
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.vakin.concurrent;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.atomic.LongAccumulator;
6+
import java.util.function.LongBinaryOperator;
7+
import java.util.stream.IntStream;
8+
9+
/**
10+
* @author Benjamin Winterberg
11+
*/
12+
public class LongAccumulator1 {
13+
14+
public static void main(String[] args) {
15+
testAccumulate();
16+
}
17+
18+
private static void testAccumulate() {
19+
LongBinaryOperator op = (x, y) -> 2 * x + y;
20+
LongAccumulator accumulator = new LongAccumulator(op, 1L);
21+
22+
ExecutorService executor = Executors.newFixedThreadPool(2);
23+
24+
IntStream.range(0, 10)
25+
.forEach(i -> executor.submit(() -> accumulator.accumulate(i)));
26+
27+
executor.shutdown();
28+
29+
System.out.format("Add: %d\n", accumulator.getThenReset());
30+
}
31+
}
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.vakin.concurrent;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.atomic.LongAdder;
6+
import java.util.stream.IntStream;
7+
8+
/**
9+
* @author Benjamin Winterberg
10+
*/
11+
public class LongAdder1 {
12+
13+
private static final int NUM_INCREMENTS = 10000;
14+
15+
private static LongAdder adder = new LongAdder();
16+
17+
public static void main(String[] args) {
18+
testIncrement();
19+
testAdd();
20+
}
21+
22+
private static void testAdd() {
23+
ExecutorService executor = Executors.newFixedThreadPool(2);
24+
25+
IntStream.range(0, NUM_INCREMENTS)
26+
.forEach(i -> executor.submit(() -> adder.add(2)));
27+
28+
executor.shutdown();
29+
30+
System.out.format("Add: %d\n", adder.sumThenReset());
31+
}
32+
33+
private static void testIncrement() {
34+
ExecutorService executor = Executors.newFixedThreadPool(2);
35+
36+
IntStream.range(0, NUM_INCREMENTS)
37+
.forEach(i -> executor.submit(adder::increment));
38+
39+
executor.shutdown();
40+
41+
System.out.format("Increment: Expected=%d; Is=%d\n", NUM_INCREMENTS, adder.sumThenReset());
42+
}
43+
}

src/io/vakin/java8/StreamTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.ArrayList;
44
import java.util.Arrays;
5+
import java.util.Comparator;
56
import java.util.List;
67
import java.util.Map;
78
import java.util.Optional;
@@ -86,6 +87,21 @@ private static void test2() {
8687

8788
System.out.println(names);
8889
}
90+
91+
private static void testFindMax(){
92+
List<Integer> intlist = Arrays.asList(1,2,3,4,5,6);
93+
//error
94+
int maxId = intlist.stream().max(Integer::max).get();
95+
System.out.println("maxId = " + maxId);
96+
97+
//right
98+
maxId = intlist.stream().mapToInt(Integer::intValue).max().getAsInt();
99+
System.out.println("maxId = " + maxId);
100+
101+
//right
102+
maxId = intlist.stream().max(Comparator.comparing(Integer::intValue)).get();
103+
System.out.println("maxId = " + maxId);
104+
}
89105

90106
static class Person {
91107
String name;

src/io/vakin/base/DoubleCheckSingleton.java renamed to src/io/vakin/other/DoubleCheckSingleton.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.vakin.base;
1+
package io.vakin.other;
22

33

44
/**

0 commit comments

Comments
 (0)