Skip to content

Commit fe118a9

Browse files
committed
Java 12 更新
1 parent 35dab05 commit fe118a9

File tree

10 files changed

+264
-9
lines changed

10 files changed

+264
-9
lines changed

README.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
- 支持脚本语言
2121
- JDBC4.0API
2222

23-
### JDK 1.7
23+
### [JDK 1.7](java-7/)
2424

2525
---
2626

2727
- 支持try-with-resources
2828
- switch语句块增加String支持
2929
- NIO2.0包
3030

31-
### **JDK 1.8**
31+
### [**JDK 1.8**](java-8/) - LTS
3232

3333
---
3434

@@ -42,7 +42,7 @@
4242

4343
使用元空间`Metaspace`代替持久代(`PermGen space`),JVM参数使用`-XX:MetaSpaceSize``-XX:MaxMetaspaceSize`设置大小
4444

45-
### JDK 9
45+
### [JDK 9](java-9/)
4646

4747
---
4848

@@ -55,7 +55,7 @@
5555
- [Optional更新](java-9/src/main/java/com/di1shuai/java9/optional/OptionalDemo.java)
5656
- JShell
5757

58-
### JDK 10
58+
### [JDK 10](java-10/)
5959

6060
---
6161

@@ -72,7 +72,7 @@
7272
- 基于时间(Time-Based)的版本控制模型
7373

7474

75-
### **JDK 11**
75+
### [**JDK 11**](java-11/) - LTS
7676

7777
---
7878

@@ -83,13 +83,22 @@
8383
- 单文件java命令直接编译运行
8484
- [HTTP Client标准化](java-11/src/main/java/com/di1shuai/java11/http/HTTPClientDemo.java)
8585

86-
### JDK 12
86+
### [JDK 12](java-12/)
8787

8888
---
8989

90-
- switch表达式
91-
- Shenandoah GC
92-
- 增强GC
90+
- [switch 增强](java-12/src/main/java/com/di1shuai/java12/switchdemo/SwitchDemo.java)
91+
- [NumberFormat - 数字的格式化](java-12/src/main/java/com/di1shuai/java12/number/NumberFormatDemo.java)
92+
- [Files.mismatch - 文件比较](java-12/src/main/java/com/di1shuai/java12/files/FilesDemo.java)
93+
- [String API更新](java-12/src/main/java/com/di1shuai/java12/string/StringDemo.java)
94+
- [Stream Teeing Collector](java-12/src/main/java/com/di1shuai/java12/stream/TeeingDemo.java)
95+
- 其他
96+
- 支持unicode 11(684个新字符、11个新blocks、7个新脚本)
97+
- JVM 常量 API (主要在新的java.lang.invoke.constant包中定义了一系列基于值的符号引用类型,能够描述每种可加载常量。)
98+
- Shenandoah GC(低暂停时间垃圾收集器)
99+
- G1 收集器提升 (可中止的混合收集集合、及时返回未使用的已分配内存)
100+
- 默认CDS档案
101+
- JMH 基准测试
93102

94103
### JDK 13
95104

java-12/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
### JDK 12
2+
3+
---
4+
5+
- [switch 增强](src/main/java/com/di1shuai/java12/switchdemo/SwitchDemo.java)
6+
- [NumberFormat - 数字的格式化](src/main/java/com/di1shuai/java12/number/NumberFormatDemo.java)
7+
- [Files.mismatch - 文件比较](src/main/java/com/di1shuai/java12/files/FilesDemo.java)
8+
- [String API更新](src/main/java/com/di1shuai/java12/string/StringDemo.java)
9+
- [Stream Teeing Collector](src/main/java/com/di1shuai/java12/stream/TeeingDemo.java)
10+
- 其他
11+
- 支持unicode 11(684个新字符、11个新blocks、7个新脚本)
12+
- JVM 常量 API (主要在新的java.lang.invoke.constant包中定义了一系列基于值的符号引用类型,能够描述每种可加载常量。)
13+
- Shenandoah GC(低暂停时间垃圾收集器)
14+
- G1 收集器提升 (可中止的混合收集集合、及时返回未使用的已分配内存)
15+
- 默认CDS档案
16+
- JMH 基准测试

java-12/pom.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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>java-versions</artifactId>
7+
<groupId>com.di1shuai</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>java-12</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>14</maven.compiler.source>
16+
<maven.compiler.target>14</maven.compiler.target>
17+
</properties>
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.projectlombok</groupId>
21+
<artifactId>lombok</artifactId>
22+
<scope>provided</scope>
23+
</dependency>
24+
</dependencies>
25+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.di1shuai.java12;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
6+
/**
7+
* @author Shea
8+
* @date 2021-01-22
9+
* @description
10+
*/
11+
@Data
12+
@AllArgsConstructor
13+
public class Student {
14+
15+
private String name;
16+
17+
private Integer age;
18+
19+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.di1shuai.java12.files;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
8+
/**
9+
* @author Shea
10+
* @date 2021-01-22
11+
* @description 文件比较 mismatch 返回第一个不匹配的位置,如果都匹配就返回-1L
12+
*/
13+
public class FilesDemo {
14+
15+
public static void main(String[] args) {
16+
Path path1 = Paths.get("/Users/shuai/Documents/GitRepo/mine/language/java-versions/java-12/src/main/java/com/di1shuai/java12/files/FilesDemo.java");
17+
Path path2 = Paths.get("/Users/shuai/Documents/GitRepo/mine/language/java-versions/java-12/src/main/java/com/di1shuai/java12/files/FilesDemo.java");
18+
19+
try {
20+
long fileMismatch = Files.mismatch(path1, path2);
21+
System.out.println(fileMismatch);
22+
} catch (IOException e) {
23+
e.printStackTrace();
24+
}
25+
}
26+
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.di1shuai.java12.number;
2+
3+
import java.text.NumberFormat;
4+
import java.util.Locale;
5+
6+
/**
7+
* @author Shea
8+
* @date 2021-01-22
9+
* @description
10+
*/
11+
public class NumberFormatDemo {
12+
13+
public static void main(String[] args) {
14+
long number = 100000;
15+
NumberFormat nfChina = NumberFormat.getCompactNumberInstance(Locale.CHINA, NumberFormat.Style.SHORT);
16+
System.out.println(nfChina.format(number));
17+
//10万
18+
NumberFormat nfUK = NumberFormat.getCompactNumberInstance(Locale.UK, NumberFormat.Style.SHORT);
19+
System.out.println(nfUK.format(number));
20+
//100K
21+
22+
}
23+
24+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.di1shuai.java12.stream;
2+
3+
import com.di1shuai.java12.Student;
4+
5+
import java.util.Arrays;
6+
import java.util.Collections;
7+
import java.util.List;
8+
import java.util.stream.Collectors;
9+
10+
/**
11+
* @author Shea
12+
* @date 2021-01-22
13+
* @description T 操作 合并两个
14+
*/
15+
public class TeeingDemo {
16+
17+
public static void main(String[] args) {
18+
List<Student> students = Arrays.asList(
19+
new Student("shea", 28),
20+
new Student("abel", 25),
21+
new Student("sean", 40)
22+
);
23+
String collect = students.stream().collect(Collectors.teeing(
24+
Collectors.averagingInt(Student::getAge),
25+
Collectors.summingInt(Student::getAge),
26+
(avgAge, sumAge) -> avgAge + " : " + sumAge
27+
));
28+
System.out.println(collect);
29+
30+
31+
}
32+
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.di1shuai.java12.string;
2+
3+
import java.util.List;
4+
5+
/**
6+
* @author Shea
7+
* @date 2021-01-22
8+
* @description API更新
9+
* <p>
10+
* transform 字符串转换,可以配合函数式接口Function一起使用
11+
* indent 缩进,每行开头增加空格space和移除空格
12+
*/
13+
public class StringDemo {
14+
15+
public static void main(String[] args) {
16+
//transform
17+
List.of(" Hello ", " Hi ").forEach(
18+
s -> {
19+
String newStr = s.transform(String::strip)
20+
.transform(e -> e + " Java 12");
21+
System.out.println(newStr);
22+
});
23+
24+
//indent
25+
String result = "Hello\nJava\n12\n".indent(3);
26+
System.out.println(result);
27+
28+
}
29+
30+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.di1shuai.java12.switchdemo;
2+
3+
/**
4+
* @author Shea
5+
* @date 2021-01-22
6+
* @description Switch 增强 (预览)
7+
* case 多个 -> xxx;
8+
*
9+
*/
10+
public class SwitchDemo {
11+
12+
enum DAY {
13+
MONDAY,
14+
TUESDAY,
15+
WEDNESDAY,
16+
THURSDAY,
17+
FRIDAY,
18+
SATURDAY,
19+
SUNDAY
20+
}
21+
22+
enum TODAY {
23+
WEEKEND_DAY,
24+
WORKING_DAY
25+
}
26+
27+
public static void main(String[] args) {
28+
DAY day = DAY.MONDAY;
29+
TODAY toDay = whatIsTodayBefore12(day);
30+
System.out.println(toDay);
31+
32+
toDay = whatIsToday12(day);
33+
System.out.println(toDay);
34+
35+
36+
}
37+
38+
private static TODAY whatIsTodayBefore12(DAY day) {
39+
// Before 12
40+
TODAY today;
41+
switch (day) {
42+
case MONDAY:
43+
case TUESDAY:
44+
case WEDNESDAY:
45+
case THURSDAY:
46+
case FRIDAY:
47+
today = TODAY.WORKING_DAY;
48+
break;
49+
case SATURDAY:
50+
case SUNDAY:
51+
today = TODAY.WEEKEND_DAY;
52+
break;
53+
default:
54+
throw new RuntimeException("N/A");
55+
}
56+
return today;
57+
}
58+
59+
60+
private static TODAY whatIsToday12(DAY day) {
61+
// 12
62+
TODAY today;
63+
switch (day) {
64+
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> today = TODAY.WORKING_DAY;
65+
case SATURDAY, SUNDAY -> today = TODAY.WEEKEND_DAY;
66+
default -> throw new IllegalArgumentException("Invalid day");
67+
}
68+
return today;
69+
}
70+
71+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<module>java-9</module>
1717
<module>java-7</module>
1818
<module>java-10</module>
19+
<module>java-12</module>
1920
</modules>
2021

2122
<dependencyManagement>

0 commit comments

Comments
 (0)