Skip to content

Commit 635b0d1

Browse files
committed
Add Stage 2 Lesson 3
1 parent 106f19a commit 635b0d1

File tree

16 files changed

+507
-4
lines changed

16 files changed

+507
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<version>1.0-SNAPSHOT</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
11-
11+
<name>「一入 Java 深似海 」系列 :: 第一期 :: 第四节</name>
1212
<artifactId>stage-1-lesson-4</artifactId>
1313

1414

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<relativePath>../pom.xml</relativePath>
1010
</parent>
1111
<modelVersion>4.0.0</modelVersion>
12-
12+
<name>「一入 Java 深似海 」系列 :: 第二期 :: 第一节</name>
1313
<artifactId>stage-2-lesson-1</artifactId>
1414

1515
<dependencies>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.segmentfault.deep.in.java.modular;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
7+
public class CollectionRemoveDemoInJava8 {
8+
9+
public static void main(String[] args) {
10+
11+
List<Integer> values = new ArrayList(List.of(1, 2, 3));
12+
13+
for (int i = 0; i < values.size(); i++) {
14+
Object value = values.get(i);
15+
values.remove(value);
16+
}
17+
18+
19+
}
20+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
<artifactId>stage-2</artifactId>
77
<groupId>com.segmentfault</groupId>
88
<version>1.0-SNAPSHOT</version>
9+
<relativePath>../pom.xml</relativePath>
910
</parent>
1011
<modelVersion>4.0.0</modelVersion>
11-
12+
<name>「一入 Java 深似海 」系列 :: 第二期 :: 第二节</name>
1213
<artifactId>stage-2-lesson-2</artifactId>
1314

1415
<build>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
<name>「一入 Java 深似海 」系列 :: 第二期 :: 第二节</name>
13+
<artifactId>stage-2-lesson-3</artifactId>
14+
15+
16+
<build>
17+
<plugins>
18+
<plugin>
19+
<groupId>org.apache.maven.plugins</groupId>
20+
<artifactId>maven-compiler-plugin</artifactId>
21+
<version>3.8.0</version>
22+
<configuration>
23+
<source>11</source>
24+
<target>11</target>
25+
</configuration>
26+
</plugin>
27+
</plugins>
28+
</build>
29+
30+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.segmentfault.deep.in.java.collection.advanced;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
public class CheckedTypeCollectionDemo {
9+
10+
public static void main(String[] args) {
11+
// List 元素类型是 java.lang.Integer
12+
List<Integer> values = new ArrayList<>(Arrays.asList(1, 2, 3));
13+
// values.add("1"); // 编译错误
14+
// 泛型是编译时检查,运行时擦写
15+
16+
// 引用 List<Integer> 类型的对象 values
17+
List referencedValues = values;
18+
19+
System.out.println(referencedValues == values);
20+
21+
referencedValues.add("A"); // 添加 "A" 进入 List<Integer> values
22+
23+
// 运行时的数据 List<Integer> == List<Object> == List
24+
// values.add("1") // 运行时允许,因为成员类型是 Object
25+
26+
for (Object value : values) {
27+
System.out.println(value);
28+
}
29+
30+
// values
31+
// [0] = 1, [1] = 2, [2] = 3, [3] = "A"
32+
// 创建时尚未检查内部的数据是否类型相同,操作时做检查,Wrapper 模式(装饰器模式)的运用
33+
// Collections.checked* 接口是弥补泛型集合在运行时中的擦写中的不足
34+
// 强约束:编译时利用 Java 泛型、运行时利用 Collections.checked* 接口
35+
List<Integer> checkedTypeValues = Collections.checkedList(values, Integer.class);
36+
// checkedTypeValues.add("1"); // 编译错误
37+
// 运行时检查
38+
39+
// 又将 checkedTypeValues 引用给 referencedValues
40+
referencedValues = checkedTypeValues;
41+
42+
System.out.println(referencedValues == values);
43+
44+
System.out.println(referencedValues == checkedTypeValues);
45+
46+
// 添加 "B" 进入 referencedValues
47+
referencedValues.add("B");
48+
49+
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.segmentfault.deep.in.java.collection.advanced;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
7+
public class CollectionRemoveDemo {
8+
9+
public static void main(String[] args) {
10+
11+
List<Integer> values = new ArrayList(List.of(1, 2, 3));
12+
13+
// 常规做法
14+
Iterator<Integer> iterator = values.iterator();
15+
while (iterator.hasNext()) {
16+
iterator.next(); // 需要执行,调整游标
17+
iterator.remove();
18+
}
19+
20+
values = new ArrayList(List.of(1, 2, 3));
21+
22+
// 成功删除
23+
int size = values.size();
24+
for (int i = 0; i < size; i++) {
25+
Object value = values.get(0);
26+
values.remove(value);
27+
System.out.println(values.size());
28+
}
29+
30+
values = new ArrayList(List.of(1, 2, 3));
31+
32+
size = values.size();
33+
for (int i = 0; i < size; i++) {
34+
values.remove(0);
35+
}
36+
37+
// 失败删除: for-each 语法
38+
values = new ArrayList(List.of(1, 2, 3));
39+
40+
for (Integer value : values) {
41+
values.remove(value);
42+
}
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.segmentfault.deep.in.java.collection.advanced;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
6+
public class EmptyCollectionDemo {
7+
8+
public static void main(String[] args) {
9+
10+
// 对自己(严格):所有的返回接口类型的方法禁止返回 null
11+
// 对别人(宽容):要做 null 判断(尤其在 RPC 场景)
12+
13+
// 集合方法入参:
14+
// 1. 如果能用 Iterable 尽量用
15+
// 2. 其次是 Collection
16+
// 3. 再者是 List 或 Set
17+
// 禁止使用具体类型,比如:ArrayList,LinkedHashSet
18+
}
19+
20+
21+
public static List<String> getIdsList(String name) {
22+
if(name ==null || name.length() < 1){
23+
return Collections.emptyList();
24+
}
25+
// 只读 empty List
26+
// 实现 Java 序列化
27+
return Collections.emptyList();
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.segmentfault.deep.in.java.collection.advanced;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.ConcurrentModificationException;
6+
import java.util.List;
7+
import java.util.concurrent.CopyOnWriteArrayList;
8+
9+
public class FailFastVsFailSafeDemo {
10+
11+
public static void main(String[] args) {
12+
13+
demoFailFast();
14+
15+
demoFailSafe();
16+
}
17+
18+
private static void demoFailSafe() {
19+
removeForEach(new CopyOnWriteArrayList<>(List.of(1, 2, 3)));
20+
}
21+
22+
private static void demoFailFast() {
23+
removeForEach(new ArrayList(List.of(1, 2, 3)));
24+
}
25+
26+
private static void removeForEach(Collection<?> values) {
27+
try {
28+
// 如果是 Fail-Fast 设计的话,那么会抛出 java.util.ConcurrentModificationException,如:ArrayList
29+
// 如果是 Fail-Safe 设计的话,那么就没有关系,如 java.util.concurrent.CopyOnWriteArrayList
30+
for (Object value : values) {
31+
values.remove(value);
32+
}
33+
System.out.println("集合删除成功,目前空间大小:" + values.size());
34+
} catch (ConcurrentModificationException e) {
35+
System.out.println("Fail-Fast 异常");
36+
}
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.segmentfault.deep.in.java.collection.advanced;
2+
3+
import java.util.HashMap;
4+
import java.util.IdentityHashMap;
5+
import java.util.Map;
6+
import java.util.Objects;
7+
8+
public class IdentityHashMapDemo {
9+
10+
public static void main(String[] args) {
11+
12+
// 如果类覆盖了 Object 的 equals(Object) 方法,那么 hashCode() 方法需不需要覆盖?
13+
// 说明:不强制覆盖,建议实现,注意不要将 hashCode() 作为 equals 方法的实现,可参考
14+
// Objects.hash(Object...) 以及 Arrays.hashCode(Object[]),hashCode() 是一个计算较重的实现
15+
// equals 通常是做对象属性的比较
16+
17+
// 如果类覆盖了 Object 的 hashCode() ,那么 equals(Object) 方法 方法需不需要覆盖?
18+
// 说明:必须实现,hashCode() 是用于 Hash 计算,比如普通的 HashMap,由于不同对象的 hashCode() 方法可能返回相同的数据
19+
// 原因一:int 数据范围 2^31-1,原因二:hashCode() 方法计算问题
20+
// 当不同对象 hashCode() 相同是,再做对象 equals(Object) 方法比较
21+
demoHashMap();
22+
23+
// 场景,需要对对象本身做鉴别
24+
demoIdentityHashMap();
25+
26+
// System.identityHashCode() 与 覆盖 hashCode() 方法的差异
27+
28+
// Object a = new Object();
29+
// demoIdentityHashCodeAndHashCode(a, a);
30+
//
31+
// Object b = new Object();
32+
// demoIdentityHashCodeAndHashCode(a, b);
33+
34+
String string1 = "1";
35+
String string2 = "1";
36+
// demoIdentityHashCodeAndHashCode(string1, string2);
37+
38+
// 不同对象
39+
string2 = new String("1");
40+
demoIdentityHashCodeAndHashCode(string1, string2);
41+
}
42+
43+
private static void demoIdentityHashCodeAndHashCode(Object a, Object b) {
44+
45+
System.out.printf("System.identityHashCode(%s) == %d \n", a, System.identityHashCode(a));
46+
System.out.printf("%s.hashCode() == %d \n", a, a.hashCode());
47+
48+
System.out.printf("System.identityHashCode(%s) == System.identityHashCode(%s) == %s \n", a, b,
49+
System.identityHashCode(a) == System.identityHashCode(b));
50+
51+
System.out.printf("%s.hashCode() == %s.hashCode() == %s \n", a, b, a.hashCode() == b.hashCode());
52+
}
53+
54+
private static void demoMap(Map<String, Integer> map) {
55+
System.out.println("A" == new String("A")); // false
56+
System.out.println("A".equals(new String("A"))); // true
57+
System.out.println("A".hashCode() == new String("A").hashCode()); // true
58+
59+
map.put("A", 1);
60+
map.put(new String("A"), 1);
61+
System.out.println(map.size());
62+
}
63+
64+
private static void demoIdentityHashMap() {
65+
demoMap(new IdentityHashMap<>());
66+
}
67+
68+
private static void demoHashMap() {
69+
demoMap(new HashMap<>());
70+
}
71+
72+
private String name;
73+
74+
private int age;
75+
76+
@Override
77+
public boolean equals(Object o) {
78+
if (this == o) return true;
79+
if (o == null || getClass() != o.getClass()) return false;
80+
IdentityHashMapDemo that = (IdentityHashMapDemo) o;
81+
return age == that.age &&
82+
Objects.equals(name, that.name);
83+
}
84+
85+
@Override
86+
public int hashCode() {
87+
return Objects.hash(name, age);
88+
}
89+
}

0 commit comments

Comments
 (0)