Skip to content

Commit da41717

Browse files
committed
feat: 添加 java-9 新特性代码(https://www.wdbyte.com/2020/02/jdk/jdk9-feature/)
1 parent 5405b20 commit da41717

File tree

7 files changed

+222
-0
lines changed

7 files changed

+222
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## core-java-9
2+
当前模块包含 Java 9 新特性相关代码
3+
4+
### 相关文章
5+
6+
- [Java 9 新特性介绍](https://www.wdbyte.com/2020/02/jdk/jdk9-feature/)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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>core-java-modules</artifactId>
7+
<groupId>com.wdbyte.core-java-modules</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<artifactId>core-java-9</artifactId>
12+
<version>1.0.0-SNAPSHOT</version>
13+
<name>core-java-9</name>
14+
<packaging>jar</packaging>
15+
16+
<properties>
17+
<maven.compiler.source>9</maven.compiler.source>
18+
<maven.compiler.target>9</maven.compiler.target>
19+
</properties>
20+
21+
</project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.wdbyte.java9;
2+
3+
import java.util.*;
4+
5+
/**
6+
* java 9 - 集合工厂方法
7+
* 集合工厂方法创建的集合都是不可变只读集合,在改变时会抛出异常。
8+
* 对于 Set 和 Map 工厂方法常见的,创建时如果有 key 值重复,也会直接抛出异常。
9+
* 对于 Set 和 Map 工厂方法常见的,每个 JVM 运行周期里遍历的顺序是不定的。
10+
*
11+
* @author 达西 - 公众号:未读代码
12+
*/
13+
public class Jdk9CollectionFactory {
14+
15+
public static void main(String[] args) {
16+
// 工厂方法创建集合
17+
List<String> stringList = List.of("a", "b", "c", "d");
18+
Set<String> stringSet = Set.of("a", "b", "c", "d");
19+
Map<String, Integer> stringIntegerMap = Map.of("key1", 1, "key2", 2, "key3", 3);
20+
Map<String, Integer> stringIntegerMap2 = Map.ofEntries(Map.entry("key1", 1), Map.entry("key2", 2));
21+
22+
// 集合输出
23+
System.out.println(stringList);
24+
System.out.println(stringSet);
25+
System.out.println(stringIntegerMap);
26+
System.out.println(stringIntegerMap2);
27+
28+
}
29+
30+
public void hashCodeOf(){
31+
// 工厂可以自由创建新的实例或者复用现有实例,所以 使用 of 创建的集合,避免 == 或者 hashCode 判断操作
32+
List<String> stringList = List.of("a", "b", "c", "d");
33+
List<String> stringList2 = List.of("a", "b", "c", "d");
34+
System.out.println(stringList.hashCode());
35+
System.out.println(stringList2.hashCode());
36+
}
37+
38+
/**
39+
* jdk 9 之前创建只读集合方式
40+
*/
41+
public void jdk9Before() {
42+
List<String> arrayList = new ArrayList<>();
43+
arrayList.add("达西");
44+
arrayList.add("未读代码");
45+
// 设置为只读集合
46+
arrayList = Collections.unmodifiableList(arrayList);
47+
// java.lang.UnsupportedOperationException
48+
// arrayList.add("test");
49+
// Set,Map 同理
50+
}
51+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.wdbyte.java9;
2+
3+
import java.io.IOException;
4+
import java.net.URI;
5+
6+
//import jdk.incubator.http.HttpClient;
7+
//import jdk.incubator.http.HttpRequest;
8+
//import jdk.incubator.http.HttpResponse;
9+
10+
/**
11+
* @author 达西 - 公众号:未读代码
12+
*/
13+
public class Jdk9Http {
14+
15+
/**
16+
* 只有在使用 JDK9 时才可以放开注释语句,不然找不到类
17+
*
18+
* @param args
19+
* @throws IOException
20+
* @throws InterruptedException
21+
*/
22+
public static void main(String[] args) throws IOException, InterruptedException {
23+
//HttpClient client = HttpClient.newHttpClient();
24+
//URI uri = URI.create("http://www.tianqiapi.com/api/xxx");
25+
//HttpRequest req = HttpRequest.newBuilder(uri).header("User-Agent", "Java").GET().build();
26+
//HttpResponse<String> resp = client.send(req, HttpResponse.BodyHandler.asString());
27+
//String body = resp.body();
28+
//System.out.println(body);
29+
}
30+
31+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.wdbyte.java9;
2+
3+
/**
4+
* @author 达西 - 公众号:未读代码
5+
*/
6+
public class Jdk9Interface {
7+
public static void main(String[] args) {
8+
ChinaPeople chinaPeople = new ChinaPeople();
9+
chinaPeople.sleep();
10+
chinaPeople.eat();
11+
chinaPeople.doXxx();
12+
}
13+
14+
}
15+
16+
class ChinaPeople implements People {
17+
@Override
18+
public void sleep() {
19+
System.out.println("躺着睡");
20+
}
21+
}
22+
23+
interface People {
24+
void sleep();
25+
26+
default void eat() {
27+
drink();
28+
}
29+
30+
default void doXxx() {
31+
drink();
32+
}
33+
34+
private void drink() {
35+
System.out.println("喝水");
36+
}
37+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.wdbyte.java9;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
import java.util.stream.Collectors;
6+
import java.util.stream.IntStream;
7+
import java.util.stream.Stream;
8+
9+
/**
10+
* * @author 达西 - 公众号:未读代码
11+
*/
12+
public class Jdk9Stream {
13+
14+
/**
15+
* takeWhile ,从头开始筛选,遇到不满足的就结束了
16+
*
17+
* @param args
18+
*/
19+
public static void main(String[] args) {
20+
// takeWhile ,从头开始筛选,遇到不满足的就结束了
21+
List<Integer> list1 = List.of(1, 2, 3, 4, 5);
22+
List<Integer> listResult = list1.stream().takeWhile(x -> x < 3).collect(Collectors.toList());
23+
System.out.println(listResult);
24+
// takeWhile ,从头开始筛选,遇到不满足的就结束
25+
List<Integer> list2 = List.of(1, 2, 3, 4, 3, 0);
26+
List<Integer> listResult2 = list2.stream().takeWhile(x -> x < 3).collect(Collectors.toList());
27+
System.out.println(listResult2);
28+
}
29+
30+
/**
31+
* dropWhile ,从头开始删除,遇到不满足的就结束了
32+
*/
33+
public void dropWhile() {
34+
// dropWhile ,从头开始删除,遇到不满足的就结束了
35+
List<Integer> list1 = List.of(1, 2, 3, 4, 5);
36+
List<Integer> listResult = list1.stream().dropWhile(x -> x < 3).collect(Collectors.toList());
37+
System.out.println(listResult);
38+
// dropWhile ,从头开始删除,遇到不满足的就结束
39+
List<Integer> list2 = List.of(1, 2, 3, 4, 3, 0);
40+
List<Integer> listResult2 = list2.stream().dropWhile(x -> x < 3).collect(Collectors.toList());
41+
System.out.println(listResult2);
42+
}
43+
44+
/**
45+
* 使用 ofNullable 创建支持 null 的 stream
46+
*/
47+
public void ofNullable() {
48+
Stream<Integer> stream = Stream.of(1, 2, null);
49+
stream.forEach(System.out::print);
50+
System.out.println();
51+
// 空指针异常
52+
// stream = Stream.of(null);
53+
stream = Stream.ofNullable(null);
54+
stream.forEach(System.out::print);
55+
}
56+
57+
/**
58+
* 重载迭代器
59+
*/
60+
public void iterate() {
61+
IntStream.iterate(0, x -> x < 10, x -> x + 1).forEach(System.out::print);
62+
}
63+
64+
/**
65+
* Optional 转 Stream
66+
*/
67+
public void optionalToStream() {
68+
Stream<Integer> s = Optional.of(1).stream();
69+
s.forEach(System.out::print);
70+
}
71+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module net.codingme.feature.jdk9 {
2+
exports com.wdbyte.java9;
3+
//requires jdk.incubator.httpclient;
4+
// 只有在使用 JDK9 时才可以放开上面的注释语句,不然找不到模块
5+
}

0 commit comments

Comments
 (0)