├── .gitignore ├── LICENSE ├── README.md ├── pom.xml └── src └── main └── java └── io └── github └── biezhi └── java11 ├── collections ├── DiamondOperatorExample.java └── Example.java ├── files └── Example.java ├── http ├── Example.java └── Foo.java ├── interfaces └── Example.java ├── processor └── Example.java ├── singlefile └── HelloWorld.java ├── string └── Example.java ├── time └── Example.java ├── trywithresources └── Example.java └── var └── Example.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Maven template 3 | target/ 4 | pom.xml.tag 5 | pom.xml.releaseBackup 6 | pom.xml.versionsBackup 7 | pom.xml.next 8 | release.properties 9 | dependency-reduced-pom.xml 10 | buildNumber.properties 11 | .mvn/timing.properties 12 | 13 | # Avoid ignoring Maven wrapper jar files (.jar files are usually ignored) 14 | !/.mvn/wrapper/maven-wrapper.jar 15 | ### JetBrains template 16 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 17 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 18 | 19 | # User-specific stuff 20 | .idea/**/workspace.xml 21 | .idea/**/tasks.xml 22 | .idea/**/dictionaries 23 | .idea/**/shelf 24 | 25 | # Sensitive or high-churn files 26 | .idea/**/dataSources/ 27 | .idea/**/dataSources.ids 28 | .idea/**/dataSources.local.xml 29 | .idea/**/sqlDataSources.xml 30 | .idea/**/dynamic.xml 31 | .idea/**/uiDesigner.xml 32 | .idea/**/dbnavigator.xml 33 | 34 | # Gradle 35 | .idea/**/gradle.xml 36 | .idea/**/libraries 37 | 38 | # CMake 39 | cmake-build-debug/ 40 | cmake-build-release/ 41 | 42 | # Mongo Explorer plugin 43 | .idea/**/mongoSettings.xml 44 | 45 | # File-based project format 46 | *.iws 47 | 48 | # IntelliJ 49 | out/ 50 | 51 | # mpeltonen/sbt-idea plugin 52 | .idea_modules/ 53 | 54 | # JIRA plugin 55 | atlassian-ide-plugin.xml 56 | 57 | # Cursive Clojure plugin 58 | .idea/replstate.xml 59 | 60 | # Crashlytics plugin (for Android Studio and IntelliJ) 61 | com_crashlytics_export_strings.xml 62 | crashlytics.properties 63 | crashlytics-build.properties 64 | fabric.properties 65 | 66 | # Editor-based Rest Client 67 | .idea/httpRequests 68 | ### VisualStudioCode template 69 | .vscode/* 70 | !.vscode/settings.json 71 | !.vscode/tasks.json 72 | !.vscode/launch.json 73 | !.vscode/extensions.json 74 | 75 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 王爵nice 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java11 Examples 2 | 3 | This repository contains some new feature code after Java8, which runs on top of JDK 11. 4 | 5 | - [var](src/main/java/io/github/biezhi/java11/var) 6 | - [string](src/main/java/io/github/biezhi/java11/string) 7 | - [collections](src/main/java/io/github/biezhi/java11/collections) 8 | - [interface](src/main/java/io/github/biezhi/java11/interfaces) 9 | - [http](src/main/java/io/github/biezhi/java11/http) 10 | - [processor](src/main/java/io/github/biezhi/java11/processor) 11 | - [try with resources](src/main/java/io/github/biezhi/java11/trywithresources) 12 | - [files](src/main/java/io/github/biezhi/java11/files) 13 | - [time](src/main/java/io/github/biezhi/java11/time) 14 | - [single file](src/main/java/io/github/biezhi/java11/singlefile) 15 | 16 | # License 17 | 18 | [MIT](LICENSE) -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | io.github.biezhi 8 | java11-examples 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | com.google.code.gson 14 | gson 15 | 2.8.5 16 | 17 | 18 | 19 | 20 | 21 | 22 | org.apache.maven.plugins 23 | maven-compiler-plugin 24 | 3.8.0 25 | 26 | 11 27 | 28 | 29 | 30 | org.apache.maven.plugins 31 | maven-surefire-plugin 32 | 2.20.1 33 | 34 | --add-modules=jdk.incubator.httpclient 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /src/main/java/io/github/biezhi/java11/collections/DiamondOperatorExample.java: -------------------------------------------------------------------------------- 1 | package io.github.biezhi.java11.collections; 2 | 3 | /** 4 | * Java7 引入了一个新的特性:Diamond Operator,来避免冗长代码和提升可读性。 5 | *

6 | * 然而在 Java 8 中,Oracle 发现在 Diamond 操作器和匿名内部类的使用中存在一些局限性, 7 | * 后来修复了这些问题并准备将其作为 Java9 的一部分发布出去。 8 | * 9 | * @author biezhi 10 | * @date 2018/7/10 11 | */ 12 | public class DiamondOperatorExample { 13 | 14 | static abstract class MyHandler { 15 | 16 | private T content; 17 | 18 | public MyHandler(T content) { 19 | this.content = content; 20 | System.out.println("构造函数收到了一条内容: " + content.toString()); 21 | } 22 | 23 | public T getContent() { 24 | return content; 25 | } 26 | 27 | public void setContent(T content) { 28 | this.content = content; 29 | } 30 | 31 | abstract void handle(); 32 | } 33 | 34 | public static void main(String[] args) { 35 | MyHandler intHandler = new MyHandler<>(1) { 36 | @Override 37 | public void handle() { 38 | System.out.println("收到红包 > " + getContent() + "元"); 39 | } 40 | }; 41 | intHandler.handle(); 42 | 43 | System.out.println("===================神奇的分割线==================="); 44 | 45 | MyHandler intHandler1 = new MyHandler<>(10) { 46 | @Override 47 | void handle() { 48 | System.out.println("收到红包 > " + getContent() + "元"); 49 | } 50 | }; 51 | intHandler1.handle(); 52 | 53 | System.out.println("===================神奇的分割线==================="); 54 | 55 | MyHandler handler = new MyHandler<>("魔法师") { 56 | @Override 57 | void handle() { 58 | System.out.println("马上把 [" + getContent() + "] 给处理了"); 59 | } 60 | }; 61 | handler.handle(); 62 | 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/io/github/biezhi/java11/collections/Example.java: -------------------------------------------------------------------------------- 1 | package io.github.biezhi.java11.collections; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | import java.util.Set; 6 | 7 | import static java.util.Map.entry; 8 | 9 | 10 | /** 11 | * 这个例子中感受一些新版本改(chao)进(xi)的集合 API。 12 | * 13 | * @author biezhi 14 | * @date 2018/7/10 15 | */ 16 | public class Example { 17 | 18 | public static void main(String[] args) { 19 | 20 | // 空列表,数据类型为 Object 21 | List immutableList = List.of(); 22 | 23 | // 创建 List 24 | var foo = List.of("biezhi", "github", "王爵的技术小黑屋"); 25 | 26 | // 空 Map,Key 和 Value 类型都是 Object 27 | Map emptyImmutableMap = Map.of(); 28 | 29 | // 快速创建一个 Map 30 | var mmp = Map.of(2017, "先赚他一个亿", 2018, "去年的梦想可能有点儿夸张"); 31 | 32 | // 使用 Entries 创建一个 Map 33 | Map emptyEntryMap = Map.ofEntries( 34 | entry(20, "装逼"), 35 | entry(30, "单身"), 36 | entry(40, "回家种地") 37 | ); 38 | 39 | // 创建一个 Map.Entry 40 | Map.Entry immutableMapEntry = Map.entry("biezhi", "emmmm"); 41 | // 其实和上面的代码片段是一样一样的 42 | Map.ofEntries(immutableMapEntry); 43 | 44 | // 创建一个空 Set 45 | Set immutableSet = Set.of(); 46 | 47 | // 快速创建一个 Set 48 | Set bar = Set.of("我", "可能", "是个", "假的", "程序员"); 49 | 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/io/github/biezhi/java11/files/Example.java: -------------------------------------------------------------------------------- 1 | package io.github.biezhi.java11.files; 2 | 3 | import java.nio.file.Files; 4 | import java.nio.file.Paths; 5 | 6 | /** 7 | * Files 读写文本文件 8 | * 9 | * @author biezhi 10 | * @date 2018/7/31 11 | */ 12 | public class Example { 13 | 14 | public static void main(String[] args) throws Exception { 15 | String text = "Hello biezhi."; 16 | 17 | // 写入文本 18 | Files.writeString(Paths.get("hello.txt"), text); 19 | 20 | // 读取文本 21 | String readText = Files.readString(Paths.get("hello.txt")); 22 | System.out.println(text.equals(readText)); 23 | 24 | // 删除文本 25 | Files.delete(Paths.get("hello.txt")); 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /src/main/java/io/github/biezhi/java11/http/Example.java: -------------------------------------------------------------------------------- 1 | package io.github.biezhi.java11.http; 2 | 3 | import com.google.gson.Gson; 4 | 5 | import java.net.*; 6 | import java.net.http.HttpClient; 7 | import java.net.http.HttpRequest; 8 | import java.net.http.HttpResponse; 9 | import java.nio.file.Files; 10 | import java.nio.file.Path; 11 | import java.nio.file.Paths; 12 | import java.util.List; 13 | import java.util.concurrent.CompletableFuture; 14 | 15 | import static java.util.stream.Collectors.toList; 16 | 17 | /** 18 | * Java 11 的 Http Client 示例 19 | *

20 | * 移除了 HttpResponse.BodyHandler.asString() 21 | * 使用 HttpResponse.BodyHandlers.ofString() 代替功能 22 | * 23 | * @author biezhi 24 | * @date 2018/7/10 25 | */ 26 | public class Example { 27 | 28 | // 同步调用 GET 29 | public static void syncGet(String uri) throws Exception { 30 | HttpClient client = HttpClient.newHttpClient(); 31 | HttpRequest request = HttpRequest.newBuilder() 32 | .uri(URI.create(uri)) 33 | .build(); 34 | 35 | HttpResponse response = 36 | client.send(request, HttpResponse.BodyHandlers.ofString()); 37 | 38 | System.out.println(response.statusCode()); 39 | System.out.println(response.body()); 40 | } 41 | 42 | // 异步调用 GET 43 | public static void asyncGet(String uri) throws Exception { 44 | HttpClient client = HttpClient.newHttpClient(); 45 | HttpRequest request = HttpRequest.newBuilder() 46 | .uri(URI.create(uri)) 47 | .build(); 48 | 49 | CompletableFuture> responseCompletableFuture = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()); 50 | responseCompletableFuture.whenComplete((resp, t) -> { 51 | if (t != null) { 52 | t.printStackTrace(); 53 | } else { 54 | System.out.println(resp.body()); 55 | System.out.println(resp.statusCode()); 56 | } 57 | }).join(); 58 | } 59 | 60 | // 异步调用 POST 61 | public static void asyncPost() throws Exception { 62 | HttpClient client = HttpClient.newHttpClient(); 63 | 64 | Gson gson = new Gson(); 65 | Foo foo = new Foo(); 66 | foo.name = "王爵nice"; 67 | foo.url = "https://github.com/biezhi"; 68 | 69 | String jsonBody = gson.toJson(foo); 70 | 71 | HttpRequest request = HttpRequest.newBuilder() 72 | .uri(new URI("https://httpbin.org/post")) 73 | .header("Content-Type", "application/json") 74 | .POST(HttpRequest.BodyPublishers.ofString(jsonBody)) 75 | .build(); 76 | client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) 77 | .whenComplete((resp, t) -> { 78 | if (t != null) { 79 | t.printStackTrace(); 80 | } else { 81 | System.out.println(resp.body()); 82 | System.out.println(resp.statusCode()); 83 | } 84 | }).join(); 85 | } 86 | 87 | // 下载文件 88 | public static void downloadFile() throws Exception { 89 | HttpClient client = HttpClient.newHttpClient(); 90 | 91 | HttpRequest request = HttpRequest.newBuilder() 92 | .uri(new URI("https://labs.consol.de/")) 93 | .GET() 94 | .build(); 95 | 96 | Path tempFile = Files.createTempFile("consol-labs-home", ".html"); 97 | HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofFile(tempFile)); 98 | System.out.println(response.statusCode()); 99 | System.out.println(response.body()); 100 | } 101 | 102 | // 上传文件 103 | public static void uploadFile() throws Exception { 104 | HttpClient client = HttpClient.newHttpClient(); 105 | 106 | 107 | HttpRequest request = HttpRequest.newBuilder() 108 | .uri(new URI("http://localhost:8080/upload/")) 109 | .POST(HttpRequest.BodyPublishers.ofFile(Paths.get("/tmp/files-to-upload.txt"))) 110 | .build(); 111 | 112 | HttpResponse response = client.send(request, HttpResponse.BodyHandlers.discarding()); 113 | System.out.println(response.statusCode()); 114 | } 115 | 116 | // 设置代理 117 | public static void proxy() throws Exception { 118 | HttpClient client = HttpClient.newBuilder() 119 | .proxy(ProxySelector.of(new InetSocketAddress("127.0.0.1", 1080))) 120 | .build(); 121 | 122 | HttpRequest request = HttpRequest.newBuilder() 123 | .uri(new URI("https://www.google.com")) 124 | .GET() 125 | .build(); 126 | 127 | HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); 128 | System.out.println(response.statusCode()); 129 | System.out.println(response.body()); 130 | } 131 | 132 | // basic 认证 133 | public static void basicAuth() throws Exception { 134 | HttpClient client = HttpClient.newBuilder() 135 | .authenticator(new Authenticator() { 136 | @Override 137 | protected PasswordAuthentication getPasswordAuthentication() { 138 | return new PasswordAuthentication("username", "password".toCharArray()); 139 | } 140 | }) 141 | .build(); 142 | 143 | HttpRequest request = HttpRequest.newBuilder() 144 | .uri(new URI("https://labs.consol.de")) 145 | .GET() 146 | .build(); 147 | 148 | HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); 149 | System.out.println(response.statusCode()); 150 | System.out.println(response.body()); 151 | } 152 | 153 | // 访问 HTTP2 网址 154 | public static void http2() throws Exception { 155 | HttpClient.newBuilder() 156 | .followRedirects(HttpClient.Redirect.NORMAL) 157 | .version(HttpClient.Version.HTTP_2) 158 | .build() 159 | .sendAsync(HttpRequest.newBuilder() 160 | .uri(new URI("https://http2.akamai.com/demo")) 161 | .GET() 162 | .build(), 163 | HttpResponse.BodyHandlers.ofString()) 164 | .whenComplete((resp, t) -> { 165 | if (t != null) { 166 | t.printStackTrace(); 167 | } else { 168 | System.out.println(resp.body()); 169 | System.out.println(resp.statusCode()); 170 | } 171 | }).join(); 172 | } 173 | 174 | // 并行请求 175 | public void getURIs(List uris) { 176 | HttpClient client = HttpClient.newHttpClient(); 177 | List requests = uris.stream() 178 | .map(HttpRequest::newBuilder) 179 | .map(HttpRequest.Builder::build) 180 | .collect(toList()); 181 | 182 | CompletableFuture.allOf(requests.stream() 183 | .map(request -> client.sendAsync(request, HttpResponse.BodyHandlers.ofString())) 184 | .toArray(CompletableFuture[]::new)) 185 | .join(); 186 | } 187 | 188 | public static void main(String[] args) throws Exception { 189 | // syncGet("https://biezhi.me"); 190 | // asyncGet("https://biezhi.me"); 191 | asyncPost(); 192 | // http2(); 193 | } 194 | 195 | } 196 | -------------------------------------------------------------------------------- /src/main/java/io/github/biezhi/java11/http/Foo.java: -------------------------------------------------------------------------------- 1 | package io.github.biezhi.java11.http; 2 | 3 | /** 4 | * @author biezhi 5 | * @date 2018/7/10 6 | */ 7 | public class Foo { 8 | 9 | String name; 10 | String url; 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/io/github/biezhi/java11/interfaces/Example.java: -------------------------------------------------------------------------------- 1 | package io.github.biezhi.java11.interfaces; 2 | 3 | /** 4 | * 接口中的私有方法 5 | *

6 | * Java 8 为接口添加了默认方法,接口可以实现行为,不止于方法签名。 7 | *

8 | * 但是,如果在接口上有几个默认方法,代码几乎相同,会导致什么情况? 9 | *

10 | * 一般我们会重构这些方法,调用一个可复用的私有方法。但默认方法不能是私有的。 11 | *

12 | * 将复用代码创建为一个默认方法不是一个好的选择,因为该辅助方法会成为公共API的一部分。 13 | *

14 | * Java9 支持向接口添加私有方法来解决这个问题。 15 | * 16 | * @author biezhi 17 | * @date 2018/7/10 18 | */ 19 | public interface Example { 20 | 21 | private static void sayHello() { 22 | System.out.println("你已经是大佬了,快和萌新打个招呼!"); 23 | } 24 | 25 | // 普通方法 26 | void normalInterfaceMethod(); 27 | 28 | // 默认方法 29 | default void interfaceMethodWithDefault() { 30 | init(); 31 | } 32 | 33 | // 另一个蛇皮默认方法 34 | default void anotherDefaultMethod() { 35 | init(); 36 | } 37 | 38 | // 这个方法不是公共 API 的一部分 39 | private void init() { 40 | System.out.println("正在给大佬准备洗脚水..."); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/io/github/biezhi/java11/processor/Example.java: -------------------------------------------------------------------------------- 1 | package io.github.biezhi.java11.processor; 2 | 3 | /** 4 | * Java SE 9 迎来一些 Process API 的改进,通过添加一些新的类和方法来优化系统级进程的管控。 5 | *

6 | * 实际上 JDK 偷偷的在 Java11 又把 API 改了一点。。。不过下面的代码是可以运行的 7 | * 8 | *

9 | * Process API 中的两个新接口: 10 | *

11 | * - java.lang.ProcessHandle 12 | * - java.lang.ProcessHandle.Info 13 | * 14 | * @author biezhi 15 | * @date 2018/7/10 16 | */ 17 | public class Example { 18 | 19 | public static void main(String[] args) { 20 | ProcessHandle currentProcess = ProcessHandle.current(); 21 | System.out.println("当前进程的 PID = " + currentProcess.pid()); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/io/github/biezhi/java11/singlefile/HelloWorld.java: -------------------------------------------------------------------------------- 1 | package io.github.biezhi.java11.singlefile; 2 | 3 | /** 4 | * 你可以在当前源码的目录下执行 5 | *

6 | * java HelloWorld.java 运行这个文件的 main 方法 7 | * 8 | * 该命令相当于 9 | * 10 | * javac -d HelloWorld.java 11 | * java -cp HelloWorld 12 | * 13 | * 该特性来自 JEP330 14 | * @author biezhi 15 | * @date 2018/8/1 16 | */ 17 | public class HelloWorld { 18 | 19 | public static void main(String[] args) { 20 | System.out.println("Hello Guys, this is Java 11."); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/io/github/biezhi/java11/string/Example.java: -------------------------------------------------------------------------------- 1 | package io.github.biezhi.java11.string; 2 | 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * String.repeat(int) 7 | * String.lines() 8 | * String.strip() 9 | * String.stripLeading() 10 | * String.stripTrailing() 11 | * String.isBlank() 12 | * 13 | * @author biezhi 14 | * @date 2018/7/10 15 | */ 16 | public class Example { 17 | 18 | /** 19 | * Write provided {@code String} in header. Note that this 20 | * implementation uses {@code String.repeat(int)}. 21 | * 22 | * @param headerText Title of header. 23 | */ 24 | private static void writeHeader(final String headerText) { 25 | final String headerSeparator = "=".repeat(headerText.length() + 4); 26 | 27 | System.out.println("\n" + headerSeparator); 28 | System.out.println(headerText); 29 | System.out.println(headerSeparator); 30 | } 31 | 32 | 33 | /** 34 | * Demonstrate method {@code String.lines()} added with JDK 11. 35 | */ 36 | public static void demonstrateStringLines() { 37 | String originalString = "Hello\nWorld\n123"; 38 | 39 | String stringWithoutLineSeparators = originalString.replaceAll("\\n", "\\\\n"); 40 | 41 | writeHeader("String.lines() on '" + stringWithoutLineSeparators + "'"); 42 | 43 | originalString.lines().forEach(System.out::println); 44 | } 45 | 46 | /** 47 | * Demonstrate method {@code String.strip()} added with JDK 11. 48 | */ 49 | public static void demonstrateStringStrip() { 50 | String originalString = " biezhi.me 23333 "; 51 | 52 | writeHeader("String.strip() on '" + originalString + "'"); 53 | System.out.println("'" + originalString.strip() + "'"); 54 | } 55 | 56 | /** 57 | * Demonstrate method {@code String.stripLeading()} added with JDK 11. 58 | */ 59 | public static void demonstrateStringStripLeading() { 60 | String originalString = " biezhi.me 23333 "; 61 | 62 | writeHeader("String.stripLeading() on '" + originalString + "'"); 63 | System.out.println("'" + originalString.stripLeading() + "'"); 64 | } 65 | 66 | /** 67 | * Demonstrate method {@code String.stripTrailing()} added with JDK 11. 68 | */ 69 | public static void demonstrateStringStripTrailing() { 70 | String originalString = " biezhi.me 23333 "; 71 | 72 | writeHeader("String.stripTrailing() on '" + originalString + "'"); 73 | System.out.println("'" + originalString.stripTrailing() + "'"); 74 | } 75 | 76 | /** 77 | * Demonstrate method {@code String.isBlank()} added with JDK 11. 78 | */ 79 | public static void demonstrateStringIsBlank() { 80 | writeHeader("String.isBlank()"); 81 | 82 | String emptyString = ""; 83 | System.out.println("空字符串 -> " + emptyString.isBlank()); 84 | 85 | String onlyLineSeparator = System.getProperty("line.separator"); 86 | System.out.println("换行符 -> " + onlyLineSeparator.isBlank()); 87 | 88 | String tabOnly = "\t"; 89 | System.out.println("Tab 制表符 -> " + tabOnly.isBlank()); 90 | 91 | String spacesOnly = " "; 92 | System.out.println("空格 -> " + spacesOnly.isBlank()); 93 | } 94 | 95 | 96 | public static void lines() { 97 | writeHeader("String.lines()"); 98 | 99 | String str = "Hello \n World, I,m\nbiezhi."; 100 | 101 | System.out.println(str.lines().collect(Collectors.toList())); 102 | } 103 | 104 | public static void main(String[] args) { 105 | // writeHeader("User-Agent\tMozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5)"); 106 | // demonstrateStringLines(); 107 | // demonstrateStringStrip(); 108 | // demonstrateStringStripLeading(); 109 | // demonstrateStringStripTrailing(); 110 | // demonstrateStringIsBlank(); 111 | lines(); 112 | } 113 | 114 | } 115 | -------------------------------------------------------------------------------- /src/main/java/io/github/biezhi/java11/time/Example.java: -------------------------------------------------------------------------------- 1 | package io.github.biezhi.java11.time; 2 | 3 | import java.time.Duration; 4 | import java.util.concurrent.TimeUnit; 5 | 6 | /** 7 | * Time convert 8 | * 9 | * @author biezhi 10 | * @date 2018/8/1 11 | */ 12 | public class Example { 13 | 14 | public static void main(String[] args) { 15 | long day = TimeUnit.DAYS.convert(Duration.ofHours(24)); 16 | System.out.println(day == 1); 17 | 18 | // 1 天 19 | System.out.println(TimeUnit.DAYS.convert(Duration.ofHours(26))); 20 | 21 | // 1 分钟 22 | System.out.println(TimeUnit.MINUTES.convert(Duration.ofSeconds(60))); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/io/github/biezhi/java11/trywithresources/Example.java: -------------------------------------------------------------------------------- 1 | package io.github.biezhi.java11.trywithresources; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.FileReader; 5 | 6 | /** 7 | * Java SE 7 引入了一个新的异常处理结构:Try-With-Resources,来自动管理资源。 8 | *

9 | * 这个新的声明结构主要目的是实现“Automatic Better Resource Management”(“自动资源管理”)。 10 | *

11 | * Java SE 9 将对这个声明作出一些改进来避免一些冗长写法,同时提高可读性。 12 | * 13 | * @author biezhi 14 | * @date 2018/7/10 15 | */ 16 | public class Example { 17 | 18 | public static void main(String[] args) throws Exception { 19 | BufferedReader reader1 = new BufferedReader(new FileReader("./README.md")); 20 | try (reader1) { 21 | while(reader1.ready()){ 22 | System.out.println(reader1.readLine()); 23 | } 24 | } 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/io/github/biezhi/java11/var/Example.java: -------------------------------------------------------------------------------- 1 | package io.github.biezhi.java11.var; 2 | 3 | import java.io.File; 4 | import java.io.FileInputStream; 5 | import java.nio.file.Files; 6 | import java.nio.file.Paths; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | /** 11 | * 感受 JEP 286 的魔法世界 12 | * 13 | * @author biezhi 14 | * @date 2018/7/10 15 | */ 16 | public class Example { 17 | 18 | public static void main(String[] args) throws Exception { 19 | var list = new ArrayList(); // 自动推断 ArrayList 20 | var stream = list.stream(); // 自动推断 Stream 21 | 22 | var newList = List.of("hello", "biezhi"); 23 | newList.forEach(System.out::println); 24 | 25 | String fileName = "./pom.xml"; 26 | 27 | var path = Paths.get(fileName); 28 | var bytes = Files.readAllBytes(path); 29 | 30 | System.out.println("字节数组: " + bytes); 31 | 32 | for (var b : bytes) { 33 | // TODO 34 | } 35 | 36 | try (var foo = new FileInputStream(new File(""))) { 37 | System.out.println(foo); 38 | } catch (Exception e) { 39 | // ignore 40 | } 41 | 42 | } 43 | } 44 | --------------------------------------------------------------------------------