Skip to content

Commit 16987cf

Browse files
authored
Add category
1 parent 2f846de commit 16987cf

File tree

1 file changed

+62
-21
lines changed

1 file changed

+62
-21
lines changed

Java/What's New in JDK8/Java8Tutorial.md

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,48 @@
11
随着 Java 8 的普及度越来越高,很多人都提到面试中关于Java 8 也是非常常问的知识点。应各位要求和需要,我打算对这部分知识做一个总结。本来准备自己总结的,后面看到Github 上有一个相关的仓库,地址:
22
[https://github.com/winterbe/java8-tutorial](https://github.com/winterbe/java8-tutorial)。这个仓库是英文的,我对其进行了翻译并添加和修改了部分内容,下面是正文了。
33

4-
# Java 8
4+
<!-- MarkdownTOC -->
5+
6+
- [Java 8 Tutorial](#java-8-tutorial)
7+
- [接口的默认方法\(Default Methods for Interfaces\)](#接口的默认方法default-methods-for-interfaces)
8+
- [Lambda表达式\(Lambda expressions\)](#lambda表达式lambda-expressions)
9+
- [函数式接口\(Functional Interfaces\)](#函数式接口functional-interfaces)
10+
- [方法和构造函数引用\(Method and Constructor References\)](#方法和构造函数引用method-and-constructor-references)
11+
- [Lamda 表达式作用域\(Lambda Scopes\)](#lamda-表达式作用域lambda-scopes)
12+
- [访问局部变量](#访问局部变量)
13+
- [访问字段和静态变量](#访问字段和静态变量)
14+
- [访问默认接口方法](#访问默认接口方法)
15+
- [内置函数式接口\(Built-in Functional Interfaces\)](#内置函数式接口built-in-functional-interfaces)
16+
- [Predicates](#predicates)
17+
- [Functions](#functions)
18+
- [Suppliers](#suppliers)
19+
- [Consumers](#consumers)
20+
- [Comparators](#comparators)
21+
- [Optionals](#optionals)
22+
- [Streams\(\)](#streams流)
23+
- [Filter\(过滤\)](#filter过滤)
24+
- [Sorted\(排序\)](#sorted排序)
25+
- [Map\(映射\)](#map映射)
26+
- [Match\(匹配\)](#match匹配)
27+
- [Count\(计数\)](#count计数)
28+
- [Reduce\(规约\)](#reduce规约)
29+
- [Parallel Streams\(并行流\)](#parallel-streams并行流)
30+
- [Sequential Sort\(串行排序\)](#sequential-sort串行排序)
31+
- [Parallel Sort\(并行排序\)](#parallel-sort并行排序)
32+
- [Maps](#maps)
33+
- [Data API\(日期相关API\)](#data-api日期相关api)
34+
- [Clock](#clock)
35+
- [Timezones\(时区\)](#timezones时区)
36+
- [LocalTime\(本地时间\)](#localtime本地时间)
37+
- [LocalDate\(本地日期\)](#localdate本地日期)
38+
- [LocalDateTime\(本地日期时间\)](#localdatetime本地日期时间)
39+
- [Annotations\(注解\)](#annotations注解)
40+
- [Whete to go from here?](#whete-to-go-from-here)
41+
42+
<!-- /MarkdownTOC -->
43+
44+
45+
# Java 8 Tutorial
546

647
欢迎阅读我对Java 8的介绍。本教程将逐步指导您完成所有新语言功能。 在简短的代码示例的基础上,您将学习如何使用默认接口方法,lambda表达式,方法引用和可重复注释。 在本文的最后,您将熟悉最新的 API 更改,如流,函数式接口(Functional Interfaces),Map 类的扩展和新的 Date API。 没有大段枯燥的文字,只有一堆注释的代码片段。
748

@@ -29,19 +70,19 @@ Formula 接口中除了抽象方法计算接口公式还定义了默认方法 `s
2970
```java
3071
public class Main {
3172

32-
public static void main(String[] args) {
33-
// TODO 通过匿名内部类方式访问接口
34-
Formula formula = new Formula() {
35-
@Override
36-
public double calculate(int a) {
37-
return sqrt(a * 100);
38-
}
39-
};
73+
public static void main(String[] args) {
74+
// TODO 通过匿名内部类方式访问接口
75+
Formula formula = new Formula() {
76+
@Override
77+
public double calculate(int a) {
78+
return sqrt(a * 100);
79+
}
80+
};
4081

41-
System.out.println(formula.calculate(100)); // 100.0
42-
System.out.println(formula.sqrt(16)); // 4.0
82+
System.out.println(formula.calculate(100)); // 100.0
83+
System.out.println(formula.sqrt(16)); // 4.0
4384

44-
}
85+
}
4586

4687
}
4788
```
@@ -102,15 +143,15 @@ Java 语言设计者们投入了大量精力来思考如何使现有的函数友
102143
```java
103144
@FunctionalInterface
104145
public interface Converter<F, T> {
105-
T convert(F from);
146+
T convert(F from);
106147
}
107148
```
108149

109150
```java
110-
// TODO 将数字字符串转换为整数类型
111-
Converter<String, Integer> converter = (from) -> Integer.valueOf(from);
112-
Integer converted = converter.convert("123");
113-
System.out.println(converted.getClass()); //class java.lang.Integer
151+
// TODO 将数字字符串转换为整数类型
152+
Converter<String, Integer> converter = (from) -> Integer.valueOf(from);
153+
Integer converted = converter.convert("123");
154+
System.out.println(converted.getClass()); //class java.lang.Integer
114155
```
115156

116157
**译者注:**大部分函数式接口都不用我们自己写,Java8都给我们实现好了,这些接口都在java.util.function包里。
@@ -120,9 +161,9 @@ public interface Converter<F, T> {
120161
前一节中的代码还可以通过静态方法引用来表示:
121162

122163
```java
123-
Converter<String, Integer> converter = Integer::valueOf;
124-
Integer converted = converter.convert("123");
125-
System.out.println(converted.getClass()); //class java.lang.Integer
164+
Converter<String, Integer> converter = Integer::valueOf;
165+
Integer converted = converter.convert("123");
166+
System.out.println(converted.getClass()); //class java.lang.Integer
126167
```
127168
Java 8允许您通过`::`关键字传递方法或构造函数的引用。 上面的示例显示了如何引用静态方法。 但我们也可以引用对象方法:
128169

@@ -755,7 +796,7 @@ System.out.println("今天是周几:"+dayOfWeek);//TUESDAY
755796
从字符串解析一个 LocalDate 类型和解析 LocalTime 一样简单,下面是使用 `DateTimeFormatter` 解析字符串的例子:
756797

757798
```java
758-
String str1 = "2014==04==12 01时06分09秒";
799+
String str1 = "2014==04==12 01时06分09秒";
759800
// 根据需要解析的日期、时间字符串定义解析所用的格式器
760801
DateTimeFormatter fomatter1 = DateTimeFormatter
761802
.ofPattern("yyyy==MM==dd HH时mm分ss秒");

0 commit comments

Comments
 (0)