Skip to content

Commit 106f19a

Browse files
committed
Add Stage 2 Lesson 2
1 parent 5a2c497 commit 106f19a

File tree

12 files changed

+403
-14
lines changed

12 files changed

+403
-14
lines changed

「一入 Java 深似海 」/代码/segmentfault/deep-in-java/stage-2/lesson-1/pom.xml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,5 @@
3434

3535
</dependencies>
3636

37-
<build>
38-
<plugins>
39-
<plugin>
40-
<groupId>org.apache.maven.plugins</groupId>
41-
<artifactId>maven-compiler-plugin</artifactId>
42-
<version>3.8.0</version>
43-
<configuration>
44-
<source>11</source>
45-
<target>11</target>
46-
</configuration>
47-
</plugin>
48-
</plugins>
49-
</build>
50-
5137

5238
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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>stage-2</artifactId>
7+
<groupId>com.segmentfault</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>stage-2-lesson-2</artifactId>
13+
14+
<build>
15+
<plugins>
16+
<plugin>
17+
<groupId>org.apache.maven.plugins</groupId>
18+
<artifactId>maven-compiler-plugin</artifactId>
19+
<version>3.8.0</version>
20+
<configuration>
21+
<source>11</source>
22+
<target>11</target>
23+
</configuration>
24+
</plugin>
25+
</plugins>
26+
</build>
27+
28+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.segmentfault.deep.in.java.collection;
2+
3+
import java.util.AbstractList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class AbstractCollectionDemo {
8+
9+
public static void main(String[] args) {
10+
11+
abstractList();
12+
}
13+
14+
private static void abstractList() {
15+
// 返回了 Arrays.ArrayList 不允许写操作,但是允许下标数据交换
16+
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
17+
// Java 1.4 断言 assertion
18+
assert numbers instanceof AbstractList;
19+
// Exception in thread "main" java.lang.UnsupportedOperationException
20+
// numbers.add(0);
21+
numbers.set(4, 0);
22+
numbers.forEach(System.out::println);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.segmentfault.deep.in.java.collection;
2+
3+
import java.util.List;
4+
5+
public class ArrayCopyDemo {
6+
7+
public static void main(String[] args) {
8+
9+
String[] strings1 = new String[]{"Hello", "World"};
10+
11+
arraycopy(strings1, strings1);
12+
13+
// value[1] = 1
14+
// value['a'] = a
15+
16+
int value = Integer.MAX_VALUE;
17+
18+
System.out.println(value + 1);
19+
System.out.println(value + 1 == Integer.MIN_VALUE);
20+
System.out.println(value + 2 == Integer.MIN_VALUE + 1);
21+
// int 在 Java 只有 4 字节(32位)
22+
23+
// OS 32位(4字节) 以及 64位(8字节)
24+
// long 和 double 是非线程安全的,两个4字节,高和低位
25+
// Java 中默认是没有正整数(无符号整数)
26+
// C unsigned int(size_t)
27+
28+
List<String> values = List.of();
29+
30+
// values.size() == 0
31+
// values.size() < 1
32+
33+
}
34+
35+
public static void arraycopy(Object[] src, Object[] destination) {
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.segmentfault.deep.in.java.collection;
2+
3+
import java.io.*;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.Objects;
7+
8+
public class DeepCloneDemo {
9+
10+
public static void main(String[] args) throws Exception {
11+
12+
ArrayList<String> values = new ArrayList<>();
13+
values.add("A");
14+
values.add("B");
15+
values.add("C");
16+
17+
System.out.println("Shallow Clone : ");
18+
// shallow clone
19+
List<String> shallowClone = (List<String>) values.clone();
20+
displayDiff(values, shallowClone);
21+
22+
System.out.println("Deep Clone : ");
23+
// deep clone
24+
List<String> deepClone = deepClone(values);
25+
displayDiff(values, deepClone);
26+
27+
// deep clone in java serialization
28+
System.out.println("Deep Clone in Java Serialization : ");
29+
List<String> deepClone2 = deepCloneInJavaSerialization(values);
30+
displayDiff(values, deepClone2);
31+
}
32+
33+
private static void displayDiff(List<String> values, List<String> clone) {
34+
for (int i = 0; i < values.size(); i++) {
35+
System.out.printf("Objects.equals : %s\n", Objects.equals(values.get(i), clone.get(i))); // true
36+
System.out.printf("Object == : %s\n", values.get(i) == clone.get(i)); // false
37+
}
38+
}
39+
40+
private static List<String> deepClone(List<String> source) {
41+
List<String> clone = new ArrayList<>(source.size());
42+
for (String value : source) {
43+
clone.add(new String(value));
44+
}
45+
return clone;
46+
}
47+
48+
private static List<String> deepCloneInJavaSerialization(List<String> source) throws IOException, ClassNotFoundException {
49+
ArrayList<String> copy = new ArrayList<>(source);
50+
51+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
52+
53+
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
54+
55+
// Copy 对象序列化
56+
objectOutputStream.writeObject(copy);
57+
58+
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
59+
60+
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
61+
62+
List<String> clone = (List<String>) objectInputStream.readObject();
63+
64+
return clone;
65+
}
66+
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.segmentfault.deep.in.java.collection;
2+
3+
import java.util.*;
4+
5+
public class LegacyCollectionDemo {
6+
7+
public static void main(String[] args) {
8+
9+
// 几乎所有遗留集合实现是线程安全
10+
vectorVsList();
11+
vectorVsStack();
12+
hashtableVsHashMap();
13+
enumerationVsEnum();
14+
bitSet();
15+
}
16+
17+
private static void bitSet() {
18+
// BitSet 用于位运算集合操作,可以搭配 NIO ByteBuffer
19+
}
20+
21+
private static void enumerationVsEnum() {
22+
// Enumeration 主要用于迭代早期实现,由于 java.util.Iterator 从 Java 1.2 引入。
23+
String value = "1,2,3";
24+
25+
StringTokenizer tokenizer = new StringTokenizer(value, ",");
26+
27+
Enumeration enumeration = tokenizer;
28+
29+
while (enumeration.hasMoreElements()) { // 等价 Iterator.hasNext()
30+
String element = String.valueOf(enumeration.nextElement()); // 等价 Iterator.next();
31+
System.out.println(element);
32+
}
33+
34+
// Iterable 从 Java 5 引入,用于 for-each 语句语法提升
35+
}
36+
37+
private static void hashtableVsHashMap() {
38+
// Hashtable 与 HashMap
39+
// Hashtable
40+
// 实现 Dictionary 和 Map 接口
41+
// 线程安全
42+
// key 与 value 不允许 null
43+
44+
// HashMap 实现 Map 接口
45+
// 线程非安全(写操作
46+
// key 与 value 允许 null 的 value
47+
48+
// 特殊:ConcurrentHashMap
49+
// key 与 value 不允许 null
50+
// 如果 value 为空的话,ConcurrentHashMap 在查询数据时,会产生歧义
51+
// 到底是值为 null,还是线程不可见
52+
53+
Map<String, Object> hashMap = new HashMap<>();
54+
hashMap.put(null, null);
55+
}
56+
57+
private static void vectorVsStack() {
58+
// Vector 是 FIFO
59+
// Stack 是 LIFO,是 Vector 的子类
60+
}
61+
62+
private static void vectorVsList() {
63+
// Vector 数组实现,对比 ArrayList,实现了 List
64+
// Vector 所有操作线程安全的,使用关键字“synchronized”修饰
65+
Vector<String> vector = new Vector<>();
66+
List<String> list = new ArrayList<>();
67+
// 如果 Vector 在方法内部使用的话, synchronized 修饰后的方法基本上没有线程同步的消耗
68+
vector.add("A");
69+
list.add("A");
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.segmentfault.deep.in.java.collection;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class ListAndSetDemo {
7+
8+
public static void main(String[] args) {
9+
10+
// HashSet 并不能保证顺序
11+
Set<String> values = new HashSet<>();
12+
// 有些场景可能让你误导
13+
// 字母场景
14+
values.add("a"); // 97
15+
values.add("b"); // 98
16+
values.add("c"); // 99
17+
values.forEach(System.out::println);
18+
19+
// 数字场景
20+
values.clear();
21+
values.add("1");
22+
values.add("2");
23+
values.add("3");
24+
values.forEach(System.out::println);
25+
26+
// ASCII 码
27+
28+
// 以上例子是 ASCII 码
29+
// HashSet 或者 HashMap 采用对象 HashCode
30+
// String hashCode 由 char[] 数组构建
31+
/**
32+
* public static int hashCode(byte[] value) {
33+
* int h = 0;
34+
* for (byte v : value) {
35+
* h = 31 * h + (v & 0xff);
36+
* }
37+
* return h;
38+
* }
39+
*/
40+
// 在 Java 中 char(2字节) 相当于 int(4字节)
41+
// 汉字通过 2个 char(4字节),用一个 int(4字节)
42+
43+
}
44+
45+
// 一致性 Hash 1 2 3
46+
// 3000 的请求,平均每个节点是一个 1000 个请求
47+
// 当 节点 1 失效,Key 1 就尝试 2 或 3
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.segmentfault.deep.in.java.collection;
2+
3+
import java.util.*;
4+
5+
public class MapAndSetDemo {
6+
7+
public static void main(String[] args) {
8+
9+
// 通常 Set 是 Map Key 的实现,Set 底层运用 Map 实现
10+
// 比如 HashSet 底层运用了 HashMap
11+
// 散列码(Hash)索引
12+
Map<String, Object> map = new HashMap<String, Object>();
13+
Set<String> set = new HashSet<String>();
14+
// tab[1] hashCode = 9 key = 'a'
15+
// tab[2] hashCode = 10
16+
// tab[3] hashCode = 9 key = 'a'
17+
// TreeSet 底层运用了 TreeMap
18+
// 二叉树索引
19+
map = new TreeMap<>();
20+
set = new TreeSet<>();
21+
22+
// Integer,String implements Comparable
23+
// 3 1 2 2
24+
// 3 1 => 1 3
25+
// (1 3) 2 => 1 2 3
26+
// (1 2 3) 2 => 1 2 2 3
27+
28+
// 一致性 Hash :https://en.wikipedia.org/wiki/Consistent_hashing
29+
30+
// 负载均衡算法:Spring Cloud 负载均衡不成熟的点 - 缺少一致性 Hash 算法
31+
// 服务节点:A B C 可以均衡服务
32+
// 3000 请求,平均 1000 个请求
33+
// 尽可能平均、支持动态扩缩容 D E -> 平均 600 请求
34+
35+
// TreeMap 实现 一致性 Hash
36+
// https://github.com/Jaskey/ConsistentHash/blob/master/src/com/github/jaskey/consistenthash/ConsistentHashRouter.java
37+
// 服务节点:A B C 可以均衡服务
38+
// 正常情况 A B C -> A
39+
// 缩容或异常 A 情况 B C -> B
40+
// C -> C
41+
42+
// 更公平的实现 :RendezvousHash
43+
// 原理:https://en.wikipedia.org/wiki/Rendezvous_hashing
44+
// 实现:https://github.com/clohfink/RendezvousHash
45+
46+
47+
// LinkedHashMap
48+
// 顺序:插入顺序(默认)、访问顺序(构造器调整)
49+
// 访问顺序:LRU
50+
51+
}
52+
}

0 commit comments

Comments
 (0)